Welcome to WebmasterWorld Guest from

Forum Moderators: buckworks & webwork

Message Too Old, No Replies

DNS vs .htaccess - what's the ideal way to configure?

A question about best practice in setting up domains

10:32 pm on Apr 15, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 14, 2008
posts: 85
votes: 0

Hi All,

First, I apologize; it's likely this question has been asked a few times before, but I can't really figure out how to best search it.

The question is: what's the best practice for setting up your zone file and .htaccess to redirect URL's to a canonical URL?

Currently I have my Zone like so:

www.example.com CNAME IN 86400 example.com
example.com A IN 86400

And then in my .htaccess I've got:

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

While this is working, it got me to thinking: Am I making a loop here? Seems like i'm essentially telling a visiting browser:

1. Go to www.example.com
2. www.example.com is actually example.com
3. example.com points to www.example.com

So, thought I'd post here and see what the consensus is.
7:32 pm on Apr 18, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 14, 2008
posts: 85
votes: 0

Hmm, not quite the flood of responses I was hoping for : ) Well, if anyone should see this and even has a link to some reading which can clarify this for me, I'd be mighty appreciative. Feel free to post it via a private message if it's not appropriate for the board.

7:59 pm on Apr 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

Posting this in the Apache sub-forum would have brought a near-instantaneous response. :)

Point all domains at your server, then add this code in the root:

DirectoryIndex index.php index.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
8:13 pm on Apr 18, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 14, 2008
posts: 85
votes: 0

Thanks g1smd, wasn't sure if it verged more on the domain side or the Apache side. If I may, I have a couple of follow-ups on your response.

When you say 'point all domains at your server', would this more verbosely be "make the A record for each domain point at the IP of your server"? I ask, because I'm trying to understand the importance of CNAME's here.

Second, in that .htaccess, would that not make it so that ANY domain pointed at the IP would be redirected to the WWW, and if so, does that imply any sort of security risk? I.E. a competitor points the domain www.spammy-widgets.com at my IP, and because of the .htaccess it resolves to my homepage with a 301.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members