Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .htaccess, redirect all domains except one

I have multiple domains on my server. I want to redirect all of them to one (example.net).

My .htaccess:

RewriteEngine on 
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

I’m redirecting all URLs on my server to one main domain, but that domain is also redirecting to itself. So www.example.net returns 301 Moved Permanently and redirects back to itself. I’m told that this isn’t good for SEO. How could I fix this?

like image 500
CyberJunkie Avatar asked Jun 15 '12 23:06

CyberJunkie


People also ask

How do I redirect my site using a .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.

What is R 301 L in htaccess?

The R=301 means that the web server returns a 301 moved permanently to the requesting browser or search engine.

Can I 301 redirect from one domain to another?

A 301 redirect is a permanent redirect from one URL to another. While they can redirect site page URLs, they can also redirect from one domain to another.


1 Answers

You need to add a Rewritecond to prevent it from redirecting when you’re already on the domain that you want. There are loads of examples online if you google it, or see the RewriteCond section of Apache’s mod_rewrite documentation.

What you’re looking for is something like:

RewriteEngine on 
Rewritecond %{HTTP_HOST} !^www\.example\.net
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
like image 91
joshOfAllTrades Avatar answered Sep 26 '22 19:09

joshOfAllTrades