Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 我的mod重写代码正确吗?_Regex_Apache_.htaccess_Mod Rewrite_Apache2 - Fatal编程技术网

Regex 我的mod重写代码正确吗?

Regex 我的mod重写代码正确吗?,regex,apache,.htaccess,mod-rewrite,apache2,Regex,Apache,.htaccess,Mod Rewrite,Apache2,我想做以下几点: 请求: http://www.mydomain.com/show/requested/path/here?some=var&some_other=var 应该转发给 index.php?s=show/requested/path/here&some=var&some\u other=var (就我所知,我认为代码的最后3行是正确的) 如果用户试图通过get请求发送名为“s”的var,则应将其转发给 index.php?s=错误 i、 e: http://www.mydomai

我想做以下几点:

请求:

http://www.mydomain.com/show/requested/path/here?some=var&some_other=var

应该转发给

index.php?s=show/requested/path/here&some=var&some\u other=var

(就我所知,我认为代码的最后3行是正确的)

如果用户试图通过get请求发送名为“s”的var,则应将其转发给

index.php?s=错误

i、 e:

  • http://www.mydomain.com/show/requested/path/here?s=var
  • http://www.mydomain.com/show/requested/path/here?a=b&s=var
  • http://www.mydomain.com/show/requested/path/here?a=b&s=var&c=d
  • http://www.mydomain.com/show/requested/path/here?a=b&s
  • http://www.mydomain.com/show/requested/path/here?s
我的问题是:下面的代码真的正确吗? 它似乎与5个案例相匹配。但我不知道它是否与其他不应该出现的情况相匹配。我的目标是不允许通过get请求发送名为“s”的var

my.htaccess的内容:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)(\?|&)s=?(.*)$ [NC]
RewriteRule ^(.*)$ index.php?s=error [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?s=$1 [L,QSA]

不,这是不正确的,因为您的第二条规则将把
?s=…
注入到请求URL中,这将在第二次传递中匹配第一条规则,并将导致“%s=”错误

将代码替换为:

RewriteCond %{QUERY_STRING} !(^|&)s=error(&|$) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+.*?[?&]s\b [NC]
RewriteRule ^ index.php?s=error [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !(^|&)s=[^&]+(&|$) [NC]
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]

请注意,
%{THE_REQUEST}
表示原始请求,并且不会随着各种重写规则的应用而更改。

不,这是不正确的,因为您的第二条规则将把
?s=…
注入请求URL,这将在第二次传递中匹配第一条规则,并将导致“?s=错误”

将代码替换为:

RewriteCond %{QUERY_STRING} !(^|&)s=error(&|$) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+.*?[?&]s\b [NC]
RewriteRule ^ index.php?s=error [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !(^|&)s=[^&]+(&|$) [NC]
RewriteRule ^(.+)$ index.php?s=$1 [L,QSA]

注意使用了
%{THE_REQUEST}
,它代表原始请求,并且不随各种重写规则的应用而改变。

hmmm我在您的解决方案中得到了非常精确的结果。如果我去掉R=301,它似乎可以工作。我的.htaccess不像我解释的那样直接位于文档根目录中,但我在下面的一些文件夹中。如果我使用301,它试图重定向到我的vm/var/www/…Oops的绝对路径,我的第一条规则中有一个输入错误,我现在已经更正了。请重试。现在我可以在get vars中用s=someting覆盖路径。是否可以在查询字符串中的任何位置显示
s=something
?我的回答是假设它是第一位的?不管怎样,我还是要纠正它。在应用了我们所有的要求之后,mod_rewrite和regex都变得有点复杂,而且
s=error
可能出现在查询字符串的任何位置。嗯,我得到了非常复杂的结果。如果我去掉R=301,它似乎可以工作。我的.htaccess不像我解释的那样直接位于文档根目录中,但我在下面的一些文件夹中。如果我使用301,它试图重定向到我的vm/var/www/…Oops的绝对路径,我的第一条规则中有一个输入错误,我现在已经更正了。请重试。现在我可以在get vars中用s=someting覆盖路径。是否可以在查询字符串中的任何位置显示
s=something
?我的回答是假设它是第一位的?不管怎样,让我来纠正一下。在应用了我们所有的需求之后,mod_rewrite和regex都变得有点复杂,而且
s=error
可能出现在查询字符串的任何位置。