.htaccess 重写为强制末尾斜杠
我的 htaccess 文件中有以下代码:
# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
当我访问 www.mydomain.com/test 时,它似乎工作正常,它将其重定向到 /test/。问题是当我访问 www.mydomain.com/test/another 时,它不会将尾部斜杠放在另一个上。
有谁知道如何修改我的代码,以使无论 URL 有多长,尾部斜杠都可以工作?
谢谢!
I have the following code in my htaccess file:
# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
That seems to work fine when I go to www.mydomain.com/test it redirects it to /test/. The problem is when I go to www.mydomain.com/test/another it doesn't put the trailing slash on another.
Does anyone know how to modify my code to make the trailing slash work no matter how long the URL is?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
一个稍微更稳健的答案,基于上面的答案:
RewriteCond
将检查以确保没有具有该名称的文件,如果没有,则执行 RewriteRule。比手动扩展列表更面向未来!A slightly more robust answer, based on the answer above:
The
RewriteCond
will check to make sure there's no files with that name, and if not, perform the RewriteRule. More future-proof than having a manual list of extensions!编辑:如果您想排除某些请求,例如 php 文件:
Edit: in the case you want to exclude some requests like for php files:
虽然死亡解决方案有效,但当您忘记将某些文件类型添加到列表中时,可能会很烦人。您可以执行此操作,以在条件中使用
!-f
强制所有不直接指向文件的 URL 结尾斜杠。While Death's solution works it can be annoying when you forget to add certain file types to the list. You can do this to force trailing slash for all URLs that do not point directly to a file using
!-f
in the condition.接受的答案对我不起作用。确实如此,来自 SEOMoz:
注意
RewriteBase /< /code> 对于每条规则。至少,当我删除它时,它就停止工作了。
The accepted answer didn't work for me. This did, from SEOMoz:
Note the
RewriteBase /
for each rule. At least, when I removed it, it stopped working.这对我来说非常有效。 (来自用户 Ajax 的评论)
其他链接的问题是我的 CSS 在应用重定向规则后停止工作,但 CSS 也可以使用下面的重写规则正常工作
完整代码
This is working perfectly for me. ( from comment of user Ajax )
The problem with other links was my CSS stopped working after applying the redirect rule but CSS is also working fine with the below rewrite rule
Complete code
这应该工作得很好。它只是检查以确保尾随字符不是斜杠并添加一个。
This should work pretty well. It just checks to make sure the trailing character isn't a slash and adds one.
这对我来说效果很好,并且不依赖于对实际文件的评估,因为有些人建议使用“-f”标志:
This works just fine for me and doesn't rely on evaluating to an actual file as some have suggested the '-f' flag:
由于规则末尾强制斜线,上面的示例对我不起作用,例如 $1$2/ 。
对我来说这个工作(我将它用于 WordPress 并重定向到 HTTPS)。您必须在 RewriteEngine 和 RewriteBase 行后面添加条件和规则行:
Above examples didn't work for me thanks to forced slash on the end of rule, e.g. $1$2/ .
For me worked this (I used it for wordpress and redirecting to HTTPS). You have to add the condition and rule lines just behind RewriteEngine and RewriteBase lines:
要强制尾部斜杠,您可以使用:
注意在
mod-rewrite
之前运行的mod-dir
模块在看到对的请求时会自动添加尾部斜杠>existant dir
,因此我们必须从规则中排除目录,否则在某些服务器上使用不带RewriteCond %{REQUEST_FILENAME} !-d
的规则,您最终会得到/dir//
或者如果您关闭了directorySlash,它可能会导致问题。上面的规则为所有请求添加尾部斜杠,包括带有扩展名的文件,如果您不希望文件带有尾部斜杠,可以通过在规则上方添加以下条件来排除它们
To force the trailing slash, you can use :
Note
mod-dir
module that runs before themod-rewrite
automatically adds a trailing slash when it sees a request for anexistant dir
,so we have to exclude directories from the rule otherwise using the rule withoutRewriteCond %{REQUEST_FILENAME} !-d
on some servers, you will end up at/dir//
or it can cause problems if you have turned off the directorySlash .The rule above adds a trailing slash to all requests including files with extension, if you dont want your files with a trailing slash, you can exclude them by adding the following condition above the Rule
将此规则添加到您的配置文件中,它对我有用
add this rule in your config file and it is working for me