How to remove (redirect) trailing slashes from URLs using .htaccess

Well… I see a lot of questions regarding SEO friendly URLs, and also regarding the problem with URLs that have trailing slashes which show the same content as the URLs without trailing slashes. Although I really hope that the search engines are smarter than treating those URLs as duplicate content, I feel there is the need to know how to redirect them to the non-trailing slashes URLs.

So here it is. You open .htaccess, and you should have in it the following:

RewriteEngine On
SetEnvIf Host yoursite.tld$ CI_ENV=production
SetEnvIf Host test.yoursite.tld$ CI_ENV=testing
SetEnvIf Host localhost$ CI_ENV=development

# remove trailing slashes from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]

RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

This should work for anything, not just CodeIgniter. Hope this helps…


  1. Hello Adrian,
    unfortunately you did not provide a “leave a reply”-button on your “learn codeigniter”-pages, so i’m trying to get my message to you this way.

    I’m currently using your (very good) article “Authentication system with Ion Auth and CI3” to setup Ion_Auth and i ran into a few problems. Maybe this can help to complete the information and make it easier for noobs like me:

    1. I installed CI 3.0.4 locally as a virtual host. After setting up User.php, Dashboard.php and MY_controller.php, directing the browser to http://localhost/dashboard did not work. I had to autoload the ion_auth library and to set $config[‘base_url’] in application/config/config.php correctly to make it work.

    2. In the sourcecode of the login-function you still can find
    $this->input->post(’email’) but it must be ‘username’

    I hope you don’t feel offended, i just want to help to make following your good article a little easier.

    with kind regards,
    Ralf from Kiel, Germany

    1. Thank you very much for this info. 🙂 I never feel offended when someone corrects me. Regarding “1.” I already had an article about removing index.php from url… I assumed that whoever gets to the tutorial regarding Ion Auth has already followed the steps in my previous tutorials (about creating a cms and/or creating a multilanguage site). Also, I didn’t want to “autoload” the ion_auth simply because the “autoload” is not really an “autoload” but a simple “load” (the files gets loaded all the time). And I don’t need to load the library all the times. I only need to load them on the pages that require special rights for editor (ie not in the front side of the site…). Regarding point 2, thank you for noticing.
      Also, you can at any time comment on the tutorial pages. They do have a comment section below. Weird… It seems that those pages do not have comment sections…

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.