1

I have example.com and example.co.uk. They both point to the same Drupal installation on my server, so both domains load the exact same site.

I am now trying to redirect the .co.uk to the .com domain. I have tried using the following lines in the .htaccess file, but it always redirects to /index.php.

RewriteCond %{HTTP_HOST} ^example.co.uk
RewriteRule ^(.*) https://example.com/$1 [P] 

The following lines didn't work either.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co.uk$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

The site redirects. but it doesn't keep the URL structure as it should. If I go to example.co.uk, I get redirected to example.com. If I go to example.co.uk/node/1, I get redirect to example.com/index.

How can I set up the correct redirect?

1 Answer 1

3

Your second variant is close to the goal. But it needs some edits. Firstly, you should escape all dots in RewriteCond, so it will be

RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]

Secondly you need to edit RewriteRule to change regular expression and append request uri:

RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

It also matters where you place the code. Your second variant is right. It's best to place code just after RewriteEngine on

Final code will be:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.