您可以在.htaccess
WordPress前端控制器之前的文件:
# Prevent rewritten requests (to the WP front-controller) from being redirected
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
# Redirect everything from "example.com" to "example.dev"
# Except for certain URLs that should stay at .com
RewriteCond %{HTTP_HOST} ^(www\\.)?example\\.com [NC]
RewriteCond %{REQUEST_URI} !^/pageA-to-stay-at-com
RewriteCond %{REQUEST_URI} !^/pageB-to-stay-at-com
RewriteCond %{REQUEST_URI} !^/pageC-to-stay-at-com
RewriteRule ^ https://example.dev%{REQUEST_URI} [R=302,L]
# Redirect certain URLs from "example.dev" to "example.com"
# - That should stay at .com (not .dev)
RewriteCond %{HTTP_HOST} ^(www\\.)?example\\.dev [NC]
RewriteCond %{REQUEST_URI} ^/pageA-to-stay-at-com [OR]
RewriteCond %{REQUEST_URI} ^/pageB-to-stay-at-com [OR]
RewriteCond %{REQUEST_URI} ^/pageC-to-stay-at-com
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L]
请注意
!
(否定)第一个规则块中CondPatterns上的前缀。这使得仅当URL不匹配时,条件才成功。但是,在第二个规则块中(否
!
前缀),请求的URL必须与声明的URL之一匹配,才能重定向回
.com
.
首先使用302(临时)重定向进行测试,并在确认一切正常后,仅更改为301(永久)-如果这是目的的话。这是为了避免潜在的缓存问题。
另一种(更好的)方法,避免重复URL:
# Prevent rewritten requests (to the WP front-controller) from being redirected
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
# Set an environment variable STAY_AT_COM if the requested URL
# is one of those that should stay at the .com domain
RewriteCond %{REQUEST_URI} =/pageA-to-stay-at-com [OR]
RewriteCond %{REQUEST_URI} =/pageB-to-stay-at-com [OR]
RewriteCond %{REQUEST_URI} =/pageC-to-stay-at-com
RewriteRule ^ - [E=STAY_AT_COM:yes]
# Nothing to change below here except for the domain names...
# Redirect everything from "example.com" to "example.dev"
# Except for the URLs that should stay at .com
RewriteCond %{HTTP_HOST} ^(www\\.)?example\\.com [NC]
RewriteCond %{ENV:STAY_AT_COM} !yes
RewriteRule ^ https://example.dev%{REQUEST_URI} [R=302,L]
# Redirect URLs back from "example.dev" that should stay at .com
RewriteCond %{HTTP_HOST} ^(www\\.)?example\\.dev [NC]
RewriteCond %{ENV:STAY_AT_COM} yes
RewriteRule ^ https://example.com%{REQUEST_URI} [R=302,L]
我已将第一个规则块中的条件更改为精确的字符串匹配(
=
前缀),而不是regex(缺少字符串锚点的结尾)。这与您正在使用的URL没有多大区别,但它更容易输入并避免将来可能发生的冲突。
<小时/>UPDATE: 我忽略了一些URL需要从两个域都可以访问,并且不能重定向。上述内容将重定向到一个或另一个(双态)。你需要三重状态。。。一个或另一个或两者兼而有之。
请尝试以下操作(在.htaccess
文件):
# Prevent rewritten requests (to the WP front-controller) from being redirected
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
# The TARGET_DOMAIN environment variable holds the desired target domain (if any)
# - for the requested URL
# eg. "example.com" or "example.dev" or empty for no redirect / accessible from both.
# Set the "default" target domain
# - Any URLs not listed below will redirect to this domain
RewriteRule ^ - [E=TARGET_DOMAIN:example.dev]
# URLs that should be redirected to (or remain at) the other domain
RewriteCond %{REQUEST_URI} ^/bio [OR]
RewriteCond %{REQUEST_URI} ^/computing [OR]
RewriteCond %{REQUEST_URI} ^/contact [OR]
RewriteCond %{REQUEST_URI} ^/donate [OR]
RewriteCond %{REQUEST_URI} ^/encrypt [OR]
RewriteCond %{REQUEST_URI} ^/genderless-pronouns [OR]
RewriteCond %{REQUEST_URI} ^/gnu-linux-controversy [OR]
RewriteCond %{REQUEST_URI} ^/legal [OR]
RewriteCond %{REQUEST_URI} ^/readings [OR]
RewriteCond %{REQUEST_URI} ^/now
RewriteRule ^ - [E=TARGET_DOMAIN:example.com]
# URLs that should not be redirected - accessible from both domains
# - Sets TARGET_DOMAIN to empty string (ie. no target domain)
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-login [OR]
RewriteCond %{REQUEST_URI} \\.(php|css|js|jpg|gif|webp)$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [E=TARGET_DOMAIN]
# Redirect to the desired TARGET_DOMAIN (if any)
# - if not already at the TARGET_DOMAIN
RewriteCond %{ENV:TARGET_DOMAIN} .
RewriteCond %{HTTP_HOST}@@%{ENV:TARGET_DOMAIN} !^([a-z0-9.-]+)@@\\1$
RewriteRule ^ https://%{ENV:TARGET_DOMAIN}%{REQUEST_URI} [R=302,L]
其他说明:
RewriteCond %{REQUEST_URI} ^/bio [OR]
我又回到了使用正则表达式(regex)匹配。这为您提供了最大的灵活性,但代价是增加了复杂性(尽管您可以将两者混合使用)。以上匹配以开头的任何URL
/bio
. 所以,它也会匹配
/bio/
和
/bioanything
. 使用绳端锚(
$
) 匹配
/bio
只有例如:。
^/bio$
.
需要使用正则表达式来避免重定向任何启动的URL/wp-admin
.
RewriteCond %{ENV:TARGET_DOMAIN} .
RewriteCond %{HTTP_HOST}@@%{ENV:TARGET_DOMAIN} !^([a-z0-9.-]+)@@\\1$
RewriteRule ^ https://%{ENV:TARGET_DOMAIN}%{REQUEST_URI} [R=302,L]
第一个条件确保我们仅在存在目标域集时尝试重定向。第二个条件检查当前请求的域(即
Host
标头)与TARGET\\u域不同。如果这两个检查都成功,则会重定向到同一URL上的目标域。
与之前一样,在测试之前,请确保浏览器缓存已清除,并使用302(临时)重定向进行测试,以避免潜在的缓存问题。