.htaccess Rewrite to Force Trailing Slash at the end

扶醉桌前 提交于 2019-12-17 02:35:22

问题


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!


回答1:


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]



回答2:


A slightly more robust answer, based on the answer above:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$        /$1$2/ [L,R=301]

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!




回答3:


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.

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



回答4:


The accepted answer didn't work for me. This did, from SEOMoz:

# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

Note the RewriteBase / for each rule. At least, when I removed it, it stopped working.




回答5:


RewriteRule ^(.*)[^/]$ $1/ [L,R=301]

This should work pretty well. It just checks to make sure the trailing character isn't a slash and adds one.




回答6:


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

RewriteRule ^((.*)[^/])$ $1/ [L,R=301]

Complete code

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    #Force Trailing slash
    RewriteRule ^((.*)[^/])$ $1/ [L,R=301]
</IfModule> 



回答7:


This works just fine for me and doesn't rely on evaluating to an actual file as some have suggested the '-f' flag:

RewriteCond %{REQUEST_URI} !\.[a-z0-9]+$ [NC]
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]



回答8:


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:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# I added these two lines for redirect to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress`



回答9:


To force the trailing slash, you can use :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R]

Note mod-dir module that runs before the mod-rewrite automatically adds a trailing slash when it sees a request for an existant dir ,so we have to exclude directories from the rule otherwise using the rule without RewriteCond %{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

RewriteCond %{REQUEST_FILENAME} !-f



回答10:


<rule name="Remove trailing slash" stopProcessing="true">
    <match url="^([^.]+)/$" />

add this rule in your config file and it is working for me



来源:https://stackoverflow.com/questions/7780859/htaccess-rewrite-to-force-trailing-slash-at-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!