英文:
htaccess How to extract slash from initial request
问题
我在.htaccess规则方面并不是很专业。在.htaccess中如何实现以下操作:
- 初始链接:
.../upi.html/?id=1&amt=1
- 重定向到:
.../upi.html?id=1&amt=1
英文:
I'm not so professional in .htaccess rules. How to do the following in .htaccess:
- Initial:
.../upi.html/?id=1&amt=1
- To be redirected to:
.../upi.html?id=1&amt=1
答案1
得分: 1
要删除尾部的斜杠,您可以在htaccess文件中使用以下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [L,R=302]
上述代码会检查请求的URL是否以斜杠(/)结尾,并且请求的文件名不是一个目录。如果两个条件都满足,则会删除尾部的斜杠。当前设置为临时重定向(302)。如果按照您的预期工作,则可以将其更改为永久重定向(301)。
英文:
To remove the trailing slash you can use the following in your htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [L,R=302]
The above checks if the requested URL ends with a slash (/) and if the requested filename is not a directory. If both conditions are met, the trailing slash is removed. This is currently set as a temporary redirect (302). If this works as you expect then changes to a 301 redirect which makes it permanent.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论