Forum Moderators: phranque

Message Too Old, No Replies

Rewrites for mapping 2 URLs to one server space

         

laurenji

2:10 am on Dec 23, 2014 (gmt 0)

10+ Year Member



So, I am pretty new to .htaccess files. I think I've thought through this correctly, but I need some help verifying if I've got it right.

So, previously in my experience as an SEO, I've heard that you should NOT map two DNS records to the same server space, as Google will see that as two independent sites that each have duplicate content. If you have bought two domain names, you should set up two different hosting environments, one which has all of your website's files on it, and one which has only a .htaccess file to 301 that domain to your real domain, as opposed to just setting up one hosting environment and pointing both domain names at it. That way you have your "main" URL, and Google sees that the "sub" URL is not a duplicate site, but just redirects to the "main" URL.

However, I was talking with someone who was arguing that you COULD map two DNS records to the same server space, as long as you put in a rule in your .htaccess file along the lines of


RewriteCond %{HTTP_HOST} example2\.com
RewriteRule (.*) /$1 [R=301, L]


In this case, you have two URLS (example.com and example2.com). You want your main domain name to be example.com, so you point the DNS records for both URLS at the same hosting environment, and you use the above .htaccess rule to make it so that any time a request comes in for example2.com, it redirects it to the corresponding page on example.com instead, thus making it so Google sees your site as 1 site instead of 2.

Does this make sense? Am I on the right track? Would the above code work as described? I want to make sure I get this right before I attempt it on any client sites . . .

lucy24

4:16 am on Dec 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It doesn't matter if the two domains physically live on the same server, so long as you're redirecting from one to the other. Yes, a search engine is capable of finding out what server a domain lives on. But most of the time, unless there's something nefarious going on, they really don't care.

No matter where a domain lives, you'll have a domain-name-canonicalization redirect. Search this subforum and you'll find the question has come up about twice a week for the past 10-15 years, so no need to repeat the code here. Basically it says "If the requested host is anything other than my-preferred-name, redirect to the equivalent page on my-preferred-name".

Whether you want your two domains' DNS to point to the same physical space depends in part on your hosting setup and the server's directory structure. Pick the arrangement that you think will work best for you and your clients in the long run. At most, the "other" domain would physically consist of a single directory containing an htaccess file.

You'll also want to decide whether you want the registrar, nameserver and host to be three different entities.

:: detour to check something in Live Headers ::

I recently registered the dot com version of a domain whose real name is dot ca. At the time I didn't feel like clicking several extra buttons and then making another htaccess file, so instead I went to the registrar's control panel and clicked the "Redirect domain" option. This creates a page-for-page redirect with an absolute minimum of effort on my part. (I'm taking the easy approach, where host/nameserver/registrar are all the same entity.)

phranque

4:38 am on Jan 1, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, laurenji!


RewriteCond %{HTTP_HOST} example2\.com
RewriteRule (.*) /$1 [R=301, L]


it is better to "phrase" this as:
if the request is not for the canonical hostname, redirect the requested path to the canonical hostname:
# Redirect non-canonical hostname requests to example.com
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]


the regular expression for the matching condition should be as specific as possible.
the 301 substitution string should always contain the full canonical protocol and hostname specification