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

htaccess redirect root domain to subdomain with exceptions

· 52CE 我爱测

i m using wordpress and want to redirect my root domain to a new subdomain and ignore urls that contains a certain string http://www.example.com/post.html To http://subdomain.example.com/post.html And exclude http://www.example.c…

问题原文

原帖链接 · AYMEN SOUMER · CC BY-SA 4.0

i m using wordpress and want to redirect my root domain to a new subdomain and ignore urls that contains a certain string

http://www.example.com/post.html To

http://subdomain.example.com/post.html

And exclude

http://www.example.com/post-exclude.html

So that i can deploy a new blog while i am redirecting the old one

What i have tried :

RewriteCond %{HTTP_HOST} ^www\.example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule ^(.*) http://subdomain.example.com/$1 [L,R=301]

But it's redirecting to the main subdomain.example.com and ignoring the path

Full Code

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule ^(.*) http://subdomain.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

社区高分解答

原帖链接 · MrWhite · CC BY-SA 4.0

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule ^(.*) http://subdomain.example.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

The WordPress front-controller conflicts with your redirect and ultimately causes a redirect to /index.php. (Although that is not strictly the same as / - as you've stated - maybe there is something else that removes the index.php part, if it's not present in the resulting URL?)

When you initially request /post-exclude.html, the RewriteCond directive prevents the redirect, but after the request is internally rewritten to /index.php (by the WordPress front-controller below), processing then loops which then triggers the redirect to /index.php (the homepage). The internal rewrite results in the REQUEST_URI server variable being updated to /index.php (whilst the visible URL remains as /post-exclude.html).

In other words, when you request /post-exclude.html:

  1. Redirect code is bypassed because of the RewriteCond (condition) directive.
  2. Request is internally rewritten to /index.php (by the WordPress front-controller)
  3. The rewrite engine starts over... (as it does when the URL is rewritten)
  4. Now the redirect triggers since it is checking against /index.php, not /post-exclude.html (whilst the visible URL remains as /post-exclude.html).
  5. Redirects to http://subdomain.example.com/index.php.

You can avoid this second internal rewrite triggering the redirect by making sure you only target the initial request in the redirect. You can do this by either checking against THE_REQUEST instead (which contains the first line of the HTTP request headers) or the REDIRECT_STATUS environment variable (which is empty on the initial request and set to "200" - as in 200 OK HTTP status - after the first successful rewrite).

You should also avoid manually editing the code between the # BEGIN WordPress and # END WordPress comment markers as this is maintained by WordPress and your manual edits could be overwritten in a future update.

The redirect should go before the # BEGIN WordPress line.

RewriteCond %{REQUEST_URI} !exclude

Note that this condition excludes any URL that simply contains exclude (case-sensitive) anywhere in the requested URL. Whereas the example you posted looks quite specific. Note that if you only want to exclude a single URL, you don't need an additional condition as this can all be done in the RewriteRule directive.

Try the following instead:

# Redirect to subdomain.example.com, excluding some URL(s)
# NB: REDIRECT_STATUS - Check the initial request only (not the rewritten request)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com  [NC]
RewriteCond %{REQUEST_URI} !exclude
RewriteRule (.*) http://subdomain.example.com/$1 [L,R=302]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

As noted in comments, test with 302 (temporary) redirects and make sure your browser cache is cleared.

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

资料来源与适用范围

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

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

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