通配符转发所有帖子和页面,几乎没有例外

时间:2020-11-06 作者:ARH

我想从示例中移动我的域。com到示例。dev并设置通配符转发。我使用WordPress写博客,现在我希望所有的帖子和页面都移动到新的地址,但我希望一些页面可以使用我以前的地址访问。例如:

example.com/post -> example.dev/post
example.com/post2 -> example.dev/post2
[all posts be redirected]
example.com/page1 -> example.dev/page1
example.com/page2 -> example.dev/page2
[all pages be redirected]

Exceptions:
example.com/pageN -> example.com/pageN (remain Same URL)
example.dev/pageN -> example.com/pageN (redirected to previous domain)
example.com/PageX -> accessible from both domains
我想在同一个WordPress安装上使用两个URL,但有些URL(帖子和页面)应该在一个域上预览,有些在另一个域上预览,有些在两个域上预览,如前所述。

如果有一种方法可以给出域2的帖子和一些页面的URL,并给其余的域1,那就太棒了。比如,与其转发页面和帖子,不如重写它们的URL。例如,现在所有帖子都在域示例上。默认情况下为dev,但也在示例中列出。com。

我不知道如何确切地解释这一点。

这是我的电流.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:arhnotes.com]

# URLs that should be redirected to (or remain at) the other domain
RewriteCond %{REQUEST_URI} ^/bio [OR]
RewriteCond %{REQUEST_URI} ^/pages [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:alirezahayati.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
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]

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 503 /503.html
非常感谢并向Mr. White 感谢伟大的答案和帮助。非常感谢。

1 个回复
最合适的回答,由SO网友:MrWhite 整理而成

您可以在.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(临时)重定向进行测试,以避免潜在的缓存问题。

相关推荐

在UPDATE_USER_META之后WP_REDIRECT不工作

我创建了一个自定义页面,让用户能够编辑他们的个人资料。一切都很好,但我真的不知道为什么wp_redirect 不起作用my edit-profile-proccess.php。<?php $user = wp_get_current_user(); $userID = $user->ID; $has_error = false; $has_success = false; $message = array(); &#x