简体   繁体   English

在 htaccess 中重写规则写入

[英]RewriteRule writing in htaccess

I've no experience with regex, and the redirect rule I'm trying to put in my .htaccess file for a WordPress site is having no effect.我没有使用正则表达式的经验,并且我试图将重定向规则放入.htaccess文件中以用于 WordPress 站点没有任何效果。

I want to redirect:我想重定向:

https://example.com/example/?person=name

to

https://example.com/example/people/name

From reading, I figure my rule ought to be:从阅读中,我认为我的规则应该是:

RewriteRule  \?person=(.*) https://example.com/example/people/$1 [R=301,L]

What am I missing/doing wrong?我错过了什么/做错了什么?

From reading, I figure my rule ought to be:从阅读中,我认为我的规则应该是:

 RewriteRule \\?person=(.*) https://example.com/example/people/$1 [R=301,L]

You can't match the query string part of the URL using the RewriteRule pattern .您无法使用RewriteRule模式匹配 URL 的查询字符串部分。 The RewriteRule pattern matches against the requested URL-path only. RewriteRule模式仅与请求的 URL 路径匹配。 To match the query string you need to use a condition and check the QUERY_STRING server variable.要匹配查询字符串,您需要使用条件并检查QUERY_STRING服务器变量。

For example:例如:

RewriteCond %{QUERY_STRING} ^person=([^&]*)
RewriteRule ^example/$ /example/people/%1 [QSD,R=302,L]

This needs to go before the existing WordPress directives.这需要在现有的 WordPress 指令之前进行。

This matches the URL-path exactly as stated in your question, ie.这与您的问题中所述的 URL 路径完全匹配,即。 /example/ . /example/

%1 (as opposed to $1 ) is a backreference to the last matched condition, ie. %1 (与$1相反)是对最后一个匹配条件的反向引用,即。 the value of the person URL parameter, that occurs as the first URL parameter. person URL 参数的值,它作为第一个 URL 参数出现。

The QSD (Query String Discard) flag (Apache 2.4+) is required to remove the query string from the target URL.从目标 URL 中删除查询字符串需要QSD (查询字符串丢弃)标志(Apache 2.4+)。 If you are still on Apache 2.2 then append an empty query string (ie. append a ? ) to the end of the subsitution string instead.如果你仍然在Apache 2.2然后附加一个空的查询字符串(即附加一个? )到subsitution字符串,而不是结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM