0

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.

2

2 Answers 2

2

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
2
  • 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! Dec 15, 2012 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 Dec 15, 2012 at 23:48
2

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]
1
  • This is interesting, I never heard of this directive before. Thanks for chiming in. Dec 16, 2012 at 3:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .