本文介绍了.htaccess 带或不带斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要对以下重写规则做些什么才能使其有效,无论 URL 末尾是否为斜杠?
What do I need to do to the following rewrite rule to make it so it works whether or not their is a slash at the end of the URL?
即.http://mydomain.com/content/featured或者http://mydomain.com/content/featured/
RewriteRule ^content/featured/ /content/today.html
推荐答案
使用 $
标记字符串的结尾和 ?
标记前面的表达式重复零次或一次:
Use the $
to mark the end of the string and the ?
to mark the preceding expression to be repeated zero or one times:
RewriteRule ^content/featured/?$ content/today.html
但我建议您坚持使用一种符号并纠正拼写错误:
But I recommend you to stick to one notation and correct misspelled:
# remove trailing slashes
RewriteRule (.*)/$ $1 [L,R=301]
# add trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ $0/ [L,R=301]
这篇关于.htaccess 带或不带斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!