5

I would like to remove duplicate URLs by removing the trailing slash from URLs. If someone enters a URL like http://example.com/something/ it should 301 redirect to http://example.com/something.

Obviously the query string must be preserved when present.

Here is my .htaccess file at the moment (for localhost):

Options +FollowSymlinks

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~macuser/example.com/
# RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~macuser/example.com/index.php?/$1 [L]
</IfModule>

Also, please feel free to comment if the above can be improved in any way.

2 Answers 2

4

For your .htacces you can use

# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

To improve the urls on your page you can use my little plugin called 'remove_trailing_slash'. It is not yet aproved by devot-ee, but i can send you the plugin right away.

The usage is simple:

{exp:remove_trailing_slash url="www.google.nl/"} or {exp:remove_trailing_slash}www.google.nl/{/remove_trailing_slash}

http://reinos.nl/add-ons/remove-trailing-slash/

Please pm me your email so i can send it to you.

1
  • Awesome thanks that worked a charm!! I do not think that I need to process the URLs in my output HTML because those do not include trailing slashes (or at least none that I am aware of).
    – Lea Hayes
    Commented Dec 29, 2012 at 15:20
0

I used the following in my htaccess to redirect the urls with a trailing slash:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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