Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting all domain aliases to one with htaccess

I have a few domain aliases, while I only want one of them to be actually used. At the moment, I have two domains installed,

To redirect I'd like to use htaccess.

My domains currently work like so:

www.domain.com/index.html - Main domain's home page

www.secondDomain.com/index.html - Displays exactly the same home page as the main domain, but I want it to automatically rename the url to www.domain.com/index.html when it's used.

Thanks!

like image 467
Radicate Avatar asked Sep 03 '12 13:09

Radicate


People also ask

How can I redirect and rewrite my urls with an .htaccess file?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.


1 Answers

It is a simple matter of matching %{HTTP_HOST} not equal to www.domain.com and redirecting that to the canonical domain.

RewriteEngine On
# If the hostname is NOT www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
# 301 redirect to the same resource on www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
like image 81
Michael Berkowski Avatar answered Nov 04 '22 16:11

Michael Berkowski