问题原文
原帖链接 · Marius · CC BY-SA 4.0
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.
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
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]
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
