dialogik
dialogik

Reputation: 9552

How can I force a .com domain to redirect to .tld

I have two domains, let's say example.com and example.tld. But I want that users always get to the .tld domain. I want to use .htaccess and have tried

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

When I open example.com, it redirects to example.tld. But when I open example.tld I get an error

This webpage has a redirect loop

How do I have to modify the redirection statement?

Upvotes: 3

Views: 742

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270677

So you will need a RewriteCond to ensure that the subsequent RewriteRule is only applied when the requested HTTP_HOST was example.com.

RewriteEngine On
# Apply the rule if the host does not already match example.tld
RewriteCond %{HTTP_HOST} !^example\.tld$
RewriteRule (.*) http://example.tld/$1 [L,R=301,QSA]

Note that when using [R], the [QSA] is already implicit. You don't actually need to add it here but it doesn't hurt.

The above would redirect any domain other than example.tld into example.tld. If you added a third domain which you did not want to redirect (redirecting only example.com), instead of the negative match in RewriteCond, you could use a positive match for that domain only.

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://example.tld/$1 [L,R=301,QSA]

Upvotes: 3

Related Questions