.htaccess rules

Apache .htaccess rulesSome important .htaccess rules for quick lookup.

No trailing slashes

RewriteRule ^(.*)/$ /$1 [L,R=301]

No trailing slashes but exclude certain url

This rule will remove no trailing slashes but ignore the /admin url.

RewriteCond %{REQUEST_URI} !^.*admin\/$ [NC]
RewriteRule ^(.*)/$ /$1 [L,R=301]

Redirect domain without www to domain with www

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

Redirect one url to another url in same domain

RewriteRule ^url1$ /url2 [L,R=301]

It is recommended to use full domain path rather than relative path.

RewriteRule ^url1$ http://www.domain.com/url2 [L,R=301]

Redirect old directory/path to a new directory/path

If you have changed the category/directory then you might want to redirect all old category/directory to new one.

RewriteRule ^tag/(.*) /tags/$1 [R=301,L]

The above rule will redirect all urls from /tag/* to /tags/*. Make sure to add this rule at the very top of the .htaccess file before any other rule or else it wouldn’t work.

Redirect an URL containing a word anywhere in the path to another url in same domain

The following rule will do a permanent 301 redirect for any url containing word thisisme anywhere in the url.

RewriteRule thisisme /destination/url.html [L,R=301]

To Dos

How to tidy .htaccess file which contains a mixture and complex url redirects.


Leave a Reply

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