简体   繁体   English

使用.htaccess在网址中添加斜杠

[英]Add trailing slash to url using .htaccess

I would like to add trailing slash for deep level of my urls 我想在网址的深层添加结尾斜杠

So let's say i have this url 所以说我有这个网址

mysite.com/work mysite.com/work

when user access that url, i want to be redirected to: 当用户访问该网址时,我想重定向至:

mysite.com/work/ (this i want to happen) mysite.com/work/(这是我想发生的)

But i want this only deep levels, not for .html pages 但是我只希望这很深层次,而不是.html页面

mysite.com/testing.html/ (i don't want this to happen) mysite.com/testing.html/(我不希望这种情况发生)

I have this .htaccess rule, but this add trailing slash to my .html pages also. 我有此.htaccess规则,但这也会在我的.html页面中添加尾部斜杠。 i don't want that. 我不要

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]

Any help? 有什么帮助吗?

You can use this rule for adding trailing slash only for non-files: 您可以使用此规则仅对非文件添加尾部斜杠:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM