Posted By

troynt on 02/02/09


Tagged

modrewrite htaccess no-www


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

halfshadows


Common .htaccess Rewrite Rules


 / Published in: Apache
 

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.

  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
Posted By: MMDeveloper on February 3, 2009

nice.. I sometimes wish apache had a "forceProperFrigginURL" flag

You need to login to post a comment.