0

I would like to remove all urls with trailing slashes using the .htaccess file. I have tried a couple peices of code suggested on other questions but does not work.

Any suggestions on how to do this by modifying the code below?

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png|pdf)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
  • For what reason do you want to remove trailing slashes, if i may ask? e.g. when visiting /blog/url_title , would expect a list to be at /blog/. Same for /blog/url_title/comments, to me it does somewhat suggest a directory or a resource. – GDmac Sep 10 '14 at 6:09
  • I agree with you but I am getting duplicate content issues in my webmaster account as urls /blog & /blog/ are seen as two separate pages by Google. – Thomas K Oct 15 '14 at 14:12
  • have you added a cannonical tag to the head? That should stop from duplicate content penalties – GDmac Oct 15 '14 at 16:24
6

This is what I usually have in my .htaccess for this (goes right after RewriteBase /):

# Remove the trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.