Redirect domainname.com to www.domainname.com

To redirect (rewrite) a subdomainless site to the www prefixed one with apache, use mod_rewrite. This must be enabled in Apache’s conf file, and allowed to override the defaults with AllowOverride in the relevant directory section. Line 5 checks to see if URL patter matches (if !not www.host.com) then line 6 rewrites it.

# If it isn't already
RewriteEngine On

# If !not www.host.com then 301 to www.host.com
RewriteCond %{http_host} !www.host.com [NC]
RewriteRule ^(.*)$ http://www.host.com/$1 [R=301,L]

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in .htaccess, apache, linux Tagged with: , , , , , ,

Leave a Reply

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

*