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

Issues with redirect rules to redirect all URLs except one page

· 52CE 我爱测

I need to add following Apache redirect rules to .htaccess : http://www.example.com to be redirected to http://www.onemoreexample.com but http://www.example.com/support should not be redirected. To achieve this, I added the redir…

问题原文

原帖链接 · Zama Ques · CC BY-SA 4.0

I need to add following Apache redirect rules to .htaccess:

http://www.example.com to be redirected to http://www.onemoreexample.com

but http://www.example.com/support should not be redirected.

To achieve this, I added the redirect rules below:

 RewriteCond %{REQUEST_URI} !^/support/
 RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

But http://www.example.com/support/ is taking me to http://www.onemoreexample.com/support/ after adding the above rule

Following rules also exist before the above rules to redirect to www.

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

My .htaccess file looks like this

   RewriteEngine On
   RewriteBase /
   RewriteRule ^index\.php$ - [L]

   # add a trailing slash to /wp-admin
   RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

   RewriteCond %{REQUEST_FILENAME} -f [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^ - [L]
   RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
   RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
   RewriteRule . index.php [L]

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

   RewriteCond %{REQUEST_URI} !^/support/
   RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

Please suggest how to resolve this.

已采纳解答

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

In isolation, the redirect directives themselves are OK, so there must be something else going on...

Having looked at your .htaccess file as a whole, with the redirect directives in-place, I'm surprised your redirect directives are actually doing anything(?!), since you've put them in the wrong place at the end of the .htaccess file, after the WordPress multisite directives.

Since there is no physical directory (or file) called /support then WordPress rewrites this request to /index.php before your redirect occurs and processing stops before your redirect is even processed. (So, how this is seemingly redirecting the request is not clear - the obvious guess would be that you are seeing a cached redirect (after some earlier testing perhaps) - or there is something else entirely that is triggering the redirect?)

Generally, external redirects should always go at the top of the .htaccess file, before any rewrites. So your .htaccess file should look something like the following instead:

RewriteEngine On
RewriteBase /

# External redirects...
RewriteCond %{REQUEST_URI} !^/support/
RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

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


# WordPress multisite rewrites...
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

You will need to ensure the browser cache is clear before testing.

UPDATE#1: It resolves but the CSS breaks

The above will also redirect any static resources (CSS, JS, images) that are not also referenced with the /support/... URL-path prefix. If the corresponding CSS files are not also located at the redirected domain then your CSS will break. This maybe what's happening here... (but, assuming these are shared/common CSS resources I would have expected this to break for all the redirected pages as well? Unless perhaps the CSS used by the /support/ page is unique?).

To exclude CSS and other static resources you would need to add another exception. For example:

# Redirect everything except "/support/..." and other static resources
RewriteCond %{REQUEST_URI} !^/support/
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|gif|png)$
RewriteRule ^(.*)$ http://www.onemoreexample.com/$1 [R=302,L]

UPDATE#2 one more issue... http://www.example.com/support/wp-admin should not be redirected too but it is getting redirected to http://www.onemoreexample.com/wp-login.php.

The issue here is that /support/wp-admin is first being redirected to /wp-login.php (no /support/ prefix) by WordPress on the same domain which will then get redirected to your other domain with the directive above. So, you will need to make an exception for /wp-login.php as well, if you don't want unauthenticated users that visit /support/wp-admin being redirected.

For example, change the first condition to read something like the following instead:

RewriteCond %{REQUEST_URI} !^/(support/|wp-login\.php$)

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

资料来源与适用范围

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

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

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