Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect if domain is not correct

I need to redirect URLs with wrong domains to the correct domain.

Pseudo code example:

if (domain != "www.correctdomain.com")
    redirect("www.correctdomain.com")

How can I do this with a .htaccess file?

like image 453
Claudio Ɯǝıs Mulas Avatar asked Dec 25 '13 20:12

Claudio Ɯǝıs Mulas


People also ask

Can I redirect a domain without hosting?

If you happen to not have a hosting plan and you would like to redirect your domain to another domain, it can be easily done using Cloudflare.


1 Answers

You can do this with an If directive...

<If "%{HTTP_HOST} != 'www.example.com'">
Redirect / http://www.example.com/
</If>

Or mod_rewrite. See http://httpd.apache.org/docs/current/rewrite/remapping.html

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/?(.*)         http://www.example.com/$1 [L,R,NE]
like image 159
JAL Avatar answered Oct 12 '22 18:10

JAL