htaccess redirect 301 有效但 RewriteRule 无效
htaccess redirect 301 is working but RewriteRule is not
之前我使用了redirect 301
语句。
现在我处于需要使用 RewriteRule
语句来使用标志的位置(在没有查询字符串的情况下重定向),
但由于某种原因,它不起作用。
这是我的 .htaccess
文件:
# BEGIN WordPress
# Директивы (строки) между `BEGIN WordPress` и `END WordPress`
# созданы автоматически и подлежат изменению только через фильтры WordPress.
# Сделанные вручную изменения между этими маркерами будут перезаписаны.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#this is working
redirect 301 /bad-link/ http://localhost:8000/
#this is not working
RewriteRule ^test/bad-link.*$ "http://localhost:8000" [NC,QSD,L,R=301]
#this is also not working
RewriteRule "test/bad-link" "http://localhost:8000" [R=301]
</IfModule>
# END WordPress
您的指令顺序错误。 mod_rewrite 重定向(即 RewriteRule
指令)需要在 之前 WordPress 前端控制器重写。通过将它们放在 WordPress 指令之后,它们将永远不会被处理,因为请求已经被重写到 WP 前端控制器(index.php
)并且处理停止。
您不应编辑 # BEGIN WordPress
/ # END WordPress
代码块内的代码,因为 WordPress 本身会尝试维护此代码块,您的编辑可能会被覆盖。
您应该将 RewriteRule
301 重定向指令 放在 # BEGIN WordPress
注释标记之前。您不需要在 WordPress 代码块内重复稍后出现的 RewriteEngine On
指令。
mod_alias Redirect
指令会“起作用”,因为它不依赖于指令相对于其他 mod_rewrite 指令的顺序。不同的 Apache 模块 运行 在请求期间独立且在不同时间出现,与配置文件中指令的顺序无关。 mod_rewrite 运行s before mod_alias 和 Redirect
总是 运行s 无论前面的 mod_rewrite 指令(除非它们触发外部重定向)。
换句话说:
# External redirects...
RewriteRule ^test/bad-link http://localhost:8000/ [NC,QSD,R=301,L]
# BEGIN WordPress
: etc.
请注意,正则表达式末尾的 .*$
是多余的。
注意:首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。
之前我使用了redirect 301
语句。
现在我处于需要使用 RewriteRule
语句来使用标志的位置(在没有查询字符串的情况下重定向),
但由于某种原因,它不起作用。
这是我的 .htaccess
文件:
# BEGIN WordPress
# Директивы (строки) между `BEGIN WordPress` и `END WordPress`
# созданы автоматически и подлежат изменению только через фильтры WordPress.
# Сделанные вручную изменения между этими маркерами будут перезаписаны.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#this is working
redirect 301 /bad-link/ http://localhost:8000/
#this is not working
RewriteRule ^test/bad-link.*$ "http://localhost:8000" [NC,QSD,L,R=301]
#this is also not working
RewriteRule "test/bad-link" "http://localhost:8000" [R=301]
</IfModule>
# END WordPress
您的指令顺序错误。 mod_rewrite 重定向(即 RewriteRule
指令)需要在 之前 WordPress 前端控制器重写。通过将它们放在 WordPress 指令之后,它们将永远不会被处理,因为请求已经被重写到 WP 前端控制器(index.php
)并且处理停止。
您不应编辑 # BEGIN WordPress
/ # END WordPress
代码块内的代码,因为 WordPress 本身会尝试维护此代码块,您的编辑可能会被覆盖。
您应该将 RewriteRule
301 重定向指令 放在 # BEGIN WordPress
注释标记之前。您不需要在 WordPress 代码块内重复稍后出现的 RewriteEngine On
指令。
mod_alias Redirect
指令会“起作用”,因为它不依赖于指令相对于其他 mod_rewrite 指令的顺序。不同的 Apache 模块 运行 在请求期间独立且在不同时间出现,与配置文件中指令的顺序无关。 mod_rewrite 运行s before mod_alias 和 Redirect
总是 运行s 无论前面的 mod_rewrite 指令(除非它们触发外部重定向)。
换句话说:
# External redirects...
RewriteRule ^test/bad-link http://localhost:8000/ [NC,QSD,R=301,L]
# BEGIN WordPress
: etc.
请注意,正则表达式末尾的 .*$
是多余的。
注意:首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。