Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting multiple domains/URLs to one, canonical domain

I've seen the trick on Apache's rewrite guide on how to redirect non-www requests to www requests:

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

Which redirects all requests for example.com to www.example.com.

But how do I add in that I want requests from example1.com, www.example1.com, example2.com, www.example2.com, etc. to redirect to www.example.com?

EDIT:

Here's the solution:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE] 

Slightly different RewriteRule and added 'RewriteEngine on'

like image 846
bafromca Avatar asked May 11 '11 15:05

bafromca


People also ask

Can I point multiple domains to same website?

Pointing two URLs to the same website is a good way to direct traffic to your site from several different domain names. You can accomplish this in two ways: either redirect one of the URLs to your primary domain, or create an alias for one of the URLs. The alias would point that domain towards your primary domain.

Can you canonical to another website?

Yes, multiple versions of domains and multiple pages can point to any one “canonical URL” of yours.

Can I use 2 domain names for 1 website?

With most registrars, it's easy to forward multiple domains to your website so you can simply create one site and then redirect visitors who type one of your other domain names to that one website.

What is cross domain canonical?

Use the cross-domain rel="canonical" link element In a situation like this, you can use the rel="canonical" link element across domains to specify the exact URL of whichever domain is preferred for indexing.


1 Answers

But how do I add in that I want requests from example1.com, www.example1.com, example2.com, www.example2.com, etc. to redirect to www.example.com?

You've already done that. Your rewrite rule states "if HTTP_HOST isn't www.example.com (and isn't blank), redirect to www.example.com".

As long as your server is set up to point the other domains at the same directory you're hosting www.example.com out of, you're all set.

like image 133
ceejayoz Avatar answered Sep 29 '22 02:09

ceejayoz