服务器(WordPress)重定向不应该重定向的文件(使用htaccess)

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

我使用.htaccess 重定向某些地址或避免重定向某些地址。基本上,我将所有内容重定向到另一个域,但我指定的某些地址除外。所以example.com/NormalPage 将重定向到example.dev/NormalPage 但是example.com/ThatPage 不会被重定向,如果有人打开example.dev/ThatPage, 它将被重定向到example.com/ThatPage. 某些地址也可以从两个域访问。例如example.comexample.dev 不会被重定向,并且两者显示相同的内容。

现在,由于这个重定向,wp-admin 其他WordPress文件系统可能(不确定)被重定向。我的主要域名是example.comexample.dev 但现在我必须使用example.dev (这是新域)。这不应该发生。

我希望这两个域都能够发布和编辑页面和文字。现在,当我想编辑页面或发布便笺时,当我按下publish 按钮或update 按钮,它告诉我我处于脱机状态,或者当我想为帖子选择一个类别时,它不会列出它们。

这是我的电流.httaccess 文件你能告诉我怎么修理这个吗?

# 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} ^/keys [OR]
RewriteCond %{REQUEST_URI} ^/legal [OR]
RewriteCond %{REQUEST_URI} ^/pages [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} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-content [OR]
RewriteCond %{REQUEST_URI} ^/wp-includes [OR]
RewriteCond %{REQUEST_URI} \\.(php|css|js|jpg|gif|webp)$ [OR,NC]
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]

# 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
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
ErrorDocument 503 /503.html

1 个回复
SO网友:MrWhite

我已将所有内容重定向到另一个域,但我指定的某些地址除外

这无疑是这里的问题。需要将其他HTTP请求添加到以下规则块中,以防止针对example.com 正在重定向到example.dev.

# 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} ^/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^/wp-content [OR]
RewriteCond %{REQUEST_URI} ^/wp-includes [OR]
# Add more conditions here...
RewriteCond %{REQUEST_URI} \\.(php|css|js|jpg|gif|webp)$ [OR,NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [E=TARGET_DOMAIN]
但是,为了确定要排除的这些附加URL,您需要检查浏览器中发出的HTTP请求。其中一些请求可能由JavaScript/AJAX触发。不幸的是,我不知道这些请求会是什么,但可能会因插件等的不同而有所不同。

或者,您改变方法,而不是重定向;“一切”;从…起example.comexample.dev (默认操作)仅重定向选定URL。例如:

一些页面URL重定向到(或保留在)example.com某些页面的URL重定向到(或保留在)example.dev英寸.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.

# The "default" is NO REDIRECT

# URLs that should be redirected to (or remain at) example.com
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} ^/keys [OR]
RewriteCond %{REQUEST_URI} ^/legal [OR]
RewriteCond %{REQUEST_URI} ^/pages [OR]
RewriteCond %{REQUEST_URI} ^/readings [OR]
RewriteCond %{REQUEST_URI} ^/now
RewriteRule ^ - [E=TARGET_DOMAIN:example.com,S=1]

# URLs that should be redirected to (or remain at) example.dev
RewriteCond %{REQUEST_URI} ^/example-one [OR]
RewriteCond %{REQUEST_URI} ^/example-two [OR]
RewriteCond %{REQUEST_URI} ^/example-three
RewriteRule ^ - [E=TARGET_DOMAIN:example.dev]

# 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
# :

相关推荐

在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