的.htaccess重写部队尾随斜线结束 [英] .htaccess Rewrite to Force Trailing Slash at the end

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

问题描述

我在我的htaccess的文件以下code:

 #部队尾随斜线
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^ [^ /] + $%{REQUEST_URI} / [L,R = 301]
 

这似乎很好地工作,当我去为 www.mydomain.com/test 它重定向到/测试/。问题是,当我去为 www.mydomain.com/test/another 它不把斜线上的另一个。

有谁知道如何修改我的code键使结尾的斜线的工作,不管URL是多久?

谢谢!

解决方案

 重写规则^(*)([^ /])$的http://%{HTTP_HOST} / $ 1 $ 2 / [L,R = 301]
 

修改:在情况下,你要排除像PHP文件的一些要求:

 的RewriteCond%{REQUEST_URI} \(PHP | HTML |?JPG | GIF)!$
重写规则^([^ /])$ HTTP(*)://%{HTTP_HOST} / $ 1 $ 2 / [L,R = 301]
 

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!

解决方案

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

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天全站免登陆