开发者

.htaccess Rewrite to Force Trailing Slash at the end

开发者 https://www.devze.com 2023-04-13 01:33 出处:网络
I have the following code in my htaccess开发者_如何学JAVA file: # Force Trailing Slash RewriteCond %{REQUEST_FILENAME} !-f

I have the following code in my htaccess开发者_如何学JAVA 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!


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!


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]


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]


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.


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> 


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.


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]


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`


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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号