.htaccess:检查查询字符串是否具有某个值,否则重定向它

enc*_*nce 4 apache .htaccess

我正在努力学习一点.htaccess,我真的很担心它能做些什么.我在网上看到了一个片段,但无法让它工作,它基本上是这样的.如果查询字符串没有特定值,则将其重定向到index.php或其他页面.我怎么做?

这会查找值苹果:

www.domain.com/somefile.php?a=apples&b=xyz
Run Code Online (Sandbox Code Playgroud)

这将重定向到index.php:

www.domain.com/somefile.php?a=stinkycheese&b=xyz
Run Code Online (Sandbox Code Playgroud)

Jon*_*Lin 16

您需要在RewriteCond中使用%{QUERY_STRING}变量.因此,例如,如果您不想重定向,如果redirect=no查询字符串中存在a ,它将如下所示:

RewriteCond %{QUERY_STRING} !(^|&)redirect=no($|&)
RewriteRule . /index.php [L]
Run Code Online (Sandbox Code Playgroud)

因此,如果RewriteCond失败(它redirect=no在查询字符串中有一个),则不要应用以下规则,该规则会重写请求URI的所有内容/index.php.您需要的实际规则可能有所不同,但使用RewriteCond和%{QUERY_STRING}是您想要的基本想法.