如何从初始请求中提取斜杠的方法

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月27日 16:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定