Apache rewrite rule for multiple domains

Category: Web

Multi domain Apache rewrite rule snippet

I have finally settled on a standard rewrite rule for most of my Apache hosted sites:

ServerName www.turnedonbydefault.com   
ServerAlias turnedonbydefault.com

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.turnedonbydefault.com$ [NC]
RewriteRule .? http://www.turnedonbydefault.com%{REQUEST_URI} [R=301,L]

Instead of complicated regexes it simply checks if the domain is not the actual one we want to use and rewrites it, preserving the current page location (URI).

Monday April 14, 2014

comments powered by Disqus