我需要在我的 RewriteRule 中包含一个可选的尾部正斜杠,即 /
到目前为止我所拥有的是
RewriteRule ^([a-zA-Z0-9]+)$ u.php?$1|$2
例如,哪个工作正常http://foo.bar/abcde将重定向到http://foo.bar/u.php?abcde并处理可能存在的任何查询字符串参数。
我需要做的是采取http://foo.bar/abcde/(带有尾部正斜杠)并重定向到http://foo.bar/u.php?abcde
因此,如果它存在,我需要在 RewriteRule 中删除 $1 中的最后一个正斜杠。我该怎么做呢?我是 apache 的新手,尝试了许多不同的正则表达式规则,但无法得到正确的结果。
就放/?
之前$
在你的模式的最后:
RewriteRule ^([a-zA-Z0-9]+)/?$ u.php?$1
但我宁愿建议您只允许一种拼写(带或不带尾部斜杠)并重定向另一种拼写:
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)