Search code examples
regexapache.htaccessmod-rewriteisapi

mod_rewrite: remove www and trailing slash


I already have a rule set up to remove www from my urls and redirect them...

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

*note-I had to put quotes around the percent sign to post this message, the actual rule does not contain them.

I now want to also strip off any trailing /

How would I do this?


Solution

  • Add an extra rule:

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    
    # remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/$ $1 [L,R=301]