大网数据广告活动
网站运营与故障问答

Redirect all URLs exactly except one URL that must be redirected to a different URL

· 52CE 我爱测

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…

问题原文

原帖链接 · 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]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2020-03-18)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。