Domain – Using naked domain in apache, no “www” on domain in httpd.conf

apache-2.2domainhttpd.confvirtualhost

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.

Best Answer

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
Related Topic