简体   繁体   English

用于查询字符串重定向的 htaccess 重写规则?

[英]htaccess rewrite rule for query string redirect?

My htaccess block of code in wordpress works fine but I can't understand the syntax despite knowing what it does.我在 wordpress 中的 htaccess 代码块工作正常,但尽管知道它的作用,但我无法理解语法。

 RewriteCond %{QUERY_STRING} "mobileSite=" [NC] RewriteRule (.*) /$1? [R=301,L]

For example a url as follows例如一个网址如下

www.example.com/?mobileSite=1 www.example.com/?mobileSite=1

will be redirected to将被重定向到

www.example.com www.example.com

The code works fine but I cant understand the actual syntax part.代码工作正常,但我无法理解实际的语法部分。 For example what does (.*) /$1? [R=301,L]例如, (.*) /$1? [R=301,L] (.*) /$1? [R=301,L] do? (.*) /$1? [R=301,L]怎么办?

There number after mobileSite is a variable between 0 and 1 but it is not included in RewriteCond %{QUERY_STRING} "mobileSite=" [NC] despite the code actually working. mobileSite之后的mobileSite是一个介于 0 和 1 之间的变量,但它不包含在RewriteCond %{QUERY_STRING} "mobileSite=" [NC]尽管代码实际有效。

Please explain to me the syntax?请向我解释语法?

Thanks谢谢

If you want to match only 0 or 1 in query parameter then use:如果您只想匹配查询参数中的01 ,请使用:

RewriteCond %{QUERY_STRING} ^mobileSite=[01]$ [NC]
RewriteRule (.*) /$1? [R=301,L]

RewriteRule Breakup: RewriteRule分解:

(.*)  # match anything in URI and group it in captured group #1
/$1   # $1 is back-reference of captured value in (.*)
R=301 # redirect with status 301
L     # Last rule
?     # ? in the end is for stripping any previous query string

RewriteCond Breakup: RewriteCond分解:

%{QUERY_STRING} # match from query string
^               # start
mobileSite=[01] # match mobileSite=0 or mobileSite=1
$               # end
[NC]            # flag for ignore case

References:参考:

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

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