的.htaccess重写部队尾随斜线结束斜线、重写、部队、结束

2023-09-02 09:36:05 作者:心若向阳 ?

我在我的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]