Skip to main content
  1. Posts/

Strip off www from URLs with mod_rewrite

·56 words·1 min·

If you need to remove subdomains from the URL that users enter to visit your website, toss this into your VirtualHost directive:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

Of course, you can tack on a subdomain too, if that’s what you need:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]