Common .htaccess Rewrite Rules


/ Published in: Apache
Save to your folder(s)

Please, be careful with trailing slash rewrite as post requests without a trailing slash will be ignored.

Be sure to test if you plan on using that one.
Not compatible with Drupal.


Copy this code and paste it in your HTML
  1. ## no-www redirect
  2. RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  3. RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
  4.  
  5. ## www redirect
  6. RewriteCond %{HTTP_HOST} ^example.com [NC]
  7. RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  8.  
  9. ## multiple domain redirect
  10. RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
  11. RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
  12.  
  13.  
  14. ## trailing slash
  15. ## BE EXTRA CAREFUL WITH THIS ONE!
  16. ## POST REQUESTS WITHOUT TRAILING SLASH
  17. ## WILL BE IGNORED!
  18. RewriteCond %{REQUEST_FILENAME} !-f
  19. RewriteCond %{REQUEST_FILENAME} !-d
  20. RewriteCond %{REQUEST_URI} !(.*)/$
  21. RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.