6

I must to change domain from example.net to example.com.

To redirect all URLs exactly (www and non-www) I use:

RewriteEngine On

RewriteCond %{HTTP_HOST} (w*)example\.net$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]

But I need one URL from the old domain to be redirected to a different URL:

example.net/en to be redirected to example.com/abc, instead of example.com/en.

1 Answer 1

5

You just need to implement your more specific redirect first, before your "generic" redirect everything else directive.

For example:

RewriteCond %{HTTP_HOST} example\.net$ [NC]
RewriteRule ^en$ https://example.com/abc [L,R=301]

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

Aside...

RewriteCond %{HTTP_HOST} (w*)example\.net$ [NC]

The (w*) prefix is superfluous here. This actually matches the domain <anything>example.net - so it still "works", but is possibly more than you need?

If you only want to match example.net or www.example.net (as stated in your question) then change this condition to read:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.net$ [NC]

Or, to catch mistyped URLs if you have a wildcard subdomain:

RewriteCond %{HTTP_HOST} ^(w+\.)?example\.net$ [NC]
1

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.