的.htaccess定期EX pression需要进行尾随正斜杠可选 [英] .htaccess regular expression need to make trailing forward slash optional
问题描述
我需要包括一个可选尾随forwardslash,这是一个/在我的重写规则
I need to include an optional trailing forwardslash, that's an /, in my RewriteRule
我至今是
RewriteRule ^([a-zA-Z0-9]+)$ u.php?$1|$2
//:
工作正常,例如 http://foo.bar/abcde 会重定向到的http://foo.bar/u.php?abcde 并处理任何查询字符串参数,这可能是present。
Which works fine, for example http://foo.bar/abcde will redirect to http://foo.bar/u.php?abcde and handles any querystring parameters that may be present.
我需要做的是采取 http://foo.bar/abcde/ (与尾随forwardslash),并重定向到 http://foo.bar/u.php?abcde
What I need to do is take http://foo.bar/abcde/ (with the trailing forwardslash) and redirect to http://foo.bar/u.php?abcde
因此,如果present,我需要删除从$ 1的最后斜线在我的重写规则。我该怎么做呢?我是新来的Apache和已经尝试了许多不同的正则表达式规则,但不能得到它的权利。
So, if its present, I need remove the final forward slash from $1 in my RewriteRule. How do I do this? I'm new to apache and have tried many different regex rules but can't get it right.
推荐答案
只要把 / <?/ code>在
$
在结束在你的模式:
Just put /?
before the $
at the end in your pattern:
RewriteRule ^([a-zA-Z0-9]+)/?$ u.php?$1
不过,我更建议你允许只有一个拼写(无论有或没有斜线)和重定向另一个:
But I would rather suggest you to allow just one spelling (either with or without trailing slash) and redirect the other one:
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]
这篇关于的.htaccess定期EX pression需要进行尾随正斜杠可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!