重定向查询字符串:mod_rewrite给出404错误,但测试人员显示(几乎)正确

时间:2018-01-25 11:12:39

标签: wordpress apache .htaccess mod-rewrite

我试图将查询字符串重定向到更友好的网址。这是在WordPress中,但我试图在htaccess中执行mod_rewrite(添加一个额外的规则,而不是触及任何WordPress规则)

这是我想要的网址:localhost / site / events / term-1 / term-2 / term-3

这是实际的网址:localhost / site / events /?tax_1 = term-1& tax_2 = term-2& tax_3 = term-3

以下是我使用的代码:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /site/
  RewriteRule ^events/([^/]*)/([^/]*)/([^/]*)$ events/?tax_1=$1&tax_2=$2&tax_3=$3 [QSA,L]
 </IfModule>

... WordPress rules here ...

当我到这里时,这给我一个404错误:localhost / site / events / term-1 / term-2 / term-3(但我已经检查过localhost / site / events /?tax_1 = term-1&amp; ; tax_2 = term-2&amp; tax_3 = term-3将转到正确的位置)。我在apache错误日志中没有收到任何错误。

当我在http://htaccess.mwl.be/上测试规则时,我从上面的输入中得到了这个结果:

http://localhost/site/event/%3Ftax_1=term-1%26tax_2=term-2%26tax_3=term-3

这似乎可能是一个编码问题,但我对这些输入和输出的编码所做的所有研究,这似乎不太可能,而且我想知道这可能是测试人员的问题。

有谁可以指出我可能出错的地方?我一直在搜索mod_rewrites的文档,我觉得我的规则写得正确。它可能是一个apache设置吗?或与WordPress发生冲突?

由于

EDIT 在规则中添加NE标志现在可以在测试仪中给出正确的结果,但我仍然在实际服务器上获得404结果。

1 个答案:

答案 0 :(得分:0)

我从未在技术上解决了我的问题,但通过使用WordPress重写规则而不是mod_rewrite,我获得了所需的功能。

对于其他任何看到我添加到我的函数文件中的人:

function custom_rewrite_rule() {
  add_rewrite_rule('^events/([^/]*)/([^/]*)/([^/]*)/?','index.php?page_id=PAGE_ID_OF_EVENTS_PAGE&tax_1=$matches[1]&tax_2=$matches[2]&tax_3=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);

function custom_rewrite_tag() {
  add_rewrite_tag('%tax_1%', '([^&]+)');
  add_rewrite_tag('%tax_2%', '([^&]+)');
  add_rewrite_tag('%tax_3%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

而不是挂钩到永久链接事件/,它需要发送到index.php,其中指定的帖子ID是页面所在的位置,因为其他自定义重写将不会首先运行,因此WordPress不会赢得#t知道什么事件/意味着什么。

第一个函数添加重写规则并将其挂钩到初始化中。第三个参数指定规则何时在运行的规则列表中运行 - 它必须是顶部,就好像它是底部一样,它不会运行。

第二个将查询变量添加为标记,以便WordPress知道自定义查询变量。