Skip to content

Instantly share code, notes, and snippets.

@breenie
Last active December 22, 2015 07:08
Show Gist options
  • Select an option

  • Save breenie/6435611 to your computer and use it in GitHub Desktop.

Select an option

Save breenie/6435611 to your computer and use it in GitHub Desktop.
Useful mod_rewrite snippets
RewriteEngine on
# Redirect from anyhost to www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
# Remove www from the request uri
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
# Redirect a particular host to somewhere else
RewriteCond %{HTTP_HOST} ^([a-z.]+)?othersite\.com [NC]
RewriteRule ^/?(.*) http://www.example.com/dir [L,R,NE]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment