htaccess 重定向 301 正在运行,但 RewriteRule 不起作用 [英] htaccess redirect 301 is working but RewriteRule is not

查看:38
本文介绍了htaccess 重定向 301 正在运行,但 RewriteRule 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用 redirect 301 语句之前.现在我需要使用 RewriteRule 语句来使用标志(重定向而不使用查询字符串),但由于某种原因,它不起作用.

这是我的 .htaccess 文件:

<预><代码># 开始 WordPress# Директивы (строки) между `BEGIN WordPress` и `END WordPress`# созданы автоматически и подлежат изменению только через фильтры WordPress.# Сделанные вручную изменения между этими маркерами будут перезаписаны.<IfModule mod_rewrite.c>重写引擎开启重写规则 .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]重写基数/重写规则 ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则./index.php [L]#这是有效的重定向 301/bad-link/http://localhost:8000/#这不起作用RewriteRule ^test/bad-link.*$ "http://localhost:8000";[NC,QSD,L,R=301]#这也不起作用重写规则测​​试/坏链接"http://localhost:8000"[R=301]</IfModule># 结束 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 before mod_alias 和 Redirect 总是运行,不管前面的 mod_rewrite 指令的结果如何(除非它们触发外部重定向).

换句话说:

# 外部重定向...RewriteRule ^test/bad-link http://localhost:8000/[NC,QSD,R=301,L]# 开始 WordPress: 等等.

请注意,正则表达式末尾的 .*$ 是多余的.

注意:首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题.

Before I used redirect 301 statement. Now I'm in a position where I need to use the RewriteRule statement to make use of the flags (redirecting without the query string), but for some reason, it's not working.

This is my .htaccess file:


# 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

解决方案

You have the directives in the wrong order. The mod_rewrite redirects (ie. RewriteRule directives) need to go before the WordPress front-controller rewrites. By placing them after the WordPress directives they are never going to be processed since the request has already been rewritten to the WP front-controller (index.php) and processing stops.

You should not edit the code inside the # BEGIN WordPress / # END WordPress code block as WordPress itself tries to maintain this code block and your edits can get overwritten.

You should place your RewriteRule 301 redirect directives before the # BEGIN WordPress comment marker. You do not need to repeat the RewriteEngine On directive that occurs later, inside the WordPress code block.

The mod_alias Redirect directive would have "worked" because it is not dependent on the ordering of directives in relation to the other mod_rewrite directives. Different Apache modules run independently and at different times during the request, regardless of the order of the directives in the config file. mod_rewrite runs before mod_alias and the Redirect always runs regardless of the outcome of the preceding mod_rewrite directives (unless they trigger an external redirect).

In other words:

# External redirects...
RewriteRule ^test/bad-link http://localhost:8000/ [NC,QSD,R=301,L]

# BEGIN WordPress
: etc.

Note that the .*$ at the end of your regex is superfluous.

NB: Test first with 302 (temporary) redirects to avoid potential caching issues.

这篇关于htaccess 重定向 301 正在运行,但 RewriteRule 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆