Incredibly there is no good tutorial or easy reference guide for using naked domains (no subdomain) as the primary URI online that I could find.

I'm trying to configure this to happen in my httpd.conf in apache. Since I'm still a relative newb to this server stuff, trying to figure it out myself all I could do was configure some nasty redirect loops and error 400's.

There's plenty of notes for the more common:

http:// --> https://

and

naked to --> www.

and a ton of .htaccess stuff (not interested)

What I want is http://www.domain.com --> http://domain.com

The most helpful thing I found was this: Multiple domains (including www-"subdomain") on apache?

I ended using the solution mentioned by ceejayoz in that post that some folks noted was messy and complicated because it got the desired result but I'd like to know the best practice for this in the future.

I'd appreciate a nudge in the right direction.

Thanks in advance.

up vote 2 down vote accepted

If you have enabled mod_rewrite, you can force it like this:

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

Alternatively, define an additional vhost with ServerName www.domain.tld and always redirect to domain.tld, either with HTML, like this:

<meta http-equiv="refresh" content="0;URL='http://domain.tld/'" />

Or inside the apache vhost configuration with

Redirect permanent / http://domain.tld
  • I tried a few of those things first, and they resulted in a redirect loop. I also tried Julien's comment from the post I point above that references the multiple alias. That works too! – Chrisdigital Dec 15 '12 at 23:45
  • If you go for solution #1, with mod_rewrite, you'll need to apply it to one vhost that matches both names. If you use Redirect or flat-file (html meta) redirection, be sure to split them in 2, and that only the www-site is the one redirecting – Mathias R. Jessen Dec 15 '12 at 23:48

There are lots of good reasons for using www.example.com rather than example.com. However, both are viable options. To do as you wish just switch the domains in the standard document for using www. domains.

Using a single virtual host for both is my approach. Define the virtual host for the desired domain, then add the other domain(s) as aliases.

The following specifications should force canonical names for the naked domain. (The rewrite condition rules are modified from the apache rewrite documentation to remove the www. from the name.)

ServerName        example.com
ServerAlias       www.example.com
UseCanonicalName  On

RewriteEngine     On
RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://example.com/$1 [L,R]
  • This is interesting, I never heard of this directive before. Thanks for chiming in. – Chrisdigital Dec 16 '12 at 3:59

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.