Drew
Drew

Reputation: 6862

.htaccess Rewrite to Force Trailing Slash at the end

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!

Upvotes: 39

Views: 100550

Answers (10)

Henrik Erlandsson
Henrik Erlandsson

Reputation: 3831

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.

Upvotes: 3

jeffbyrnes
jeffbyrnes

Reputation: 2221

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!

Upvotes: 72

Amit Verma
Amit Verma

Reputation: 41219

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

Upvotes: 0

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

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

Upvotes: -3

David Najman
David Najman

Reputation: 507

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`

Upvotes: 0

Erutan409
Erutan409

Reputation: 752

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]

Upvotes: 0

Abhishek Goel
Abhishek Goel

Reputation: 19731

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> 

Upvotes: 1

Henry Heikkinen
Henry Heikkinen

Reputation: 1158

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]

Upvotes: 7

undone
undone

Reputation: 7888

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]

Upvotes: 36

dambrisco
dambrisco

Reputation: 865

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.

Upvotes: 0

Related Questions