.htaccess 在末尾重写为强制尾随斜杠 [英] .htaccess Rewrite to Force Trailing Slash at the end

查看:32
本文介绍了.htaccess 在末尾重写为强制尾随斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 htaccess 文件中有以下代码:

I have the following code in my htaccess file:

# Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]

当我转到 www.mydomain.com/test 时,这似乎工作正常,它将它重定向到/test/.问题是当我访问 www.mydomain.com/test/another 时,它不会将尾部斜杠放在另一个上.

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.

有谁知道如何修改我的代码以使尾部斜杠不管 URL 多长都有效?

Does anyone know how to modify my code to make the trailing slash work no matter how long the URL is?

谢谢!

推荐答案

RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

编辑:如果您想排除某些请求,例如对 php 文件的请求:

Edit: in the case you want to exclude some requests like for php files:

RewriteCond %{REQUEST_URI}  !.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

这篇关于.htaccess 在末尾重写为强制尾随斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆