.htaccess Rewrite to Force Trailing Slash at the end
Solution 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]
Solution 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!
Solution 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]
Solution 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.
Solution 5
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>
Drew
My name is Drew and I am a front end web developer always looking to learn more!
Updated on January 07, 2020Comments
-
Drew over 4 years
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!
-
Eruant about 11 yearsYou may also want to include css and js. This is very useful!
-
moobot almost 11 yearsI tried this, but it is adding a trailing slash on files that end in .html
-
moobot almost 11 yearsI tried this, but it is adding a trailing slash on files that end in .html
-
jeffbyrnes almost 11 yearsIf the URL leads to a real file, it shouldn't be hitting this rule; are you unnecessarily adding
.html
to your "nice" URLs? -
moobot almost 11 yearsNot me, but I guess Magento is! Thanks for the reply, I did however find a redirect which is only adding it to URLs without extensions: paulund.co.uk/using-htaccess-to-force-trailing-slash
-
jeffbyrnes almost 11 yearsAh, yeah, Magento does that by default. There's lots of great SEO/usability reasons to change that behavior of Magento's, if you can.
-
Daniel W. about 10 years@undone Might I ask what the
?
after html stands for, and why you didn't putphp?
with the question mark? -
undone about 10 years@DanFromGermany question mark in regexp indicates that preceding character (in this case l) , may or may not exists in the string. so in can cover both htm and html extensions!
-
Martie almost 10 yearsThis answer also worked wonders for me. Only problem I ran into was my redirect pointing to a weird server path. I fixed this by adding
RewriteBase /
. This made the redirect work like a charm! -
Tomas Gonzalez almost 10 yearsThis worked perfectly for me. Thanks! When trying this or any solution, remember to clear your borwser's cache.
-
dnns over 9 yearsPerfect. Thanks a lot!
-
manikandan krishnamoorthy almost 8 years<rule name="Remove trailing slash" stopProcessing="true"> <match url="^([^.]+)/$" />
-
Timothy almost 8 yearsYou should add the line of code to your answer instead of commenting it !
-
Mattypants over 7 yearsI tried this as well, but still see trailing slashes added to pages like sitemap.xml. Is it possible that the server settings decide what is viewed as a file to be downloaded and one to be rendered in the browser, and this affects how
!-f
is interpreted? -
Phil almost 4 yearsThis solution adds a slash to files as well!