【问题标题】:%26 in query string is breaking the $_GET part查询字符串中的 %26 破坏了 $_GET 部分
【发布时间】:2014-05-01 22:31:01
【问题描述】:

对于这个页面

www.industrialstores.com/search_results/Bell+%26+Gossett+101238+1.25"+Sweat+ChecktroLFlangeset/46

我在$_GET['s'] 中没有获得价值

$_GET['s'] 中的值应该是Bell Gossett 101238 1.25" Sweat ChecktroLFlangeset,但只有Bell 被传递到$_GET['s']

我已经尝试过urldecode()htmlentities,但没用。

我的 .httaccess 文件是这样的

RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L]
RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L]
RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5&r=$6 [L]

【问题讨论】:

  • 你不应该在$_GET 中得到任何东西,你没有? 来开始查询字符串
  • 损坏的 mod_rewrite?您的重写似乎不喜欢%:尝试将[a-zA-Z0-9_-] 块更改为[a-zA-Z0-9%_-]
  • 这些是 mod_rewrite RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([^/]+)?$ index\.php ?page=$1&s=$2&o=$3 [L] RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+ )/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L] RewriteRule ^([a-zA-Z0-9_-]{3,100})/( [^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([^/]+)?$ index\.php?page=$1 &s=$2&o=$3&p=$4&q=$5 [L] 重写规则 ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_ ]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9]+)/([^/]+)?$ index\.php?page=$1&s=$2 &o=$3&p=$4&q=$5&r=$6 [L]
  • 你可以改变你的正则表达式来搜索直到它找到一个斜线,就像你对第一行所做的那样。这样,它就不会破坏您的支票。像... RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5&r=$6 [L]

标签: php query-string


【解决方案1】:

正如 Second Rikudo 所写,您的重写规则不接受 & 字符 (%26)。

试试这个;

RewriteRule ^([a-zA-Z0-9_-\&]{3,100})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L]
RewriteRule ^([a-zA-Z0-9_-\&]{3,100})/([^/]+)/([a-zA-Z0-9_\&]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L]
RewriteRule ^([a-zA-Z0-9_-\&]{3,100})/([^/]+)/([a-zA-Z0-9_\&]+)/([a-zA-Z0-9_\&]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L]
RewriteRule ^([a-zA-Z0-9_-\&]{3,100})/([^/]+)/([a-zA-Z0-9_\&]+)/([a-zA-Z0-9_\&]+)/([a-zA-Z0-9\&]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5&r=$6 [L]

此外,这些重写规则并不是很有效。我会选择这样的东西;

RewriteRule ^(.*)$ index.php?route=$1 [L]

然后在 PHP 中进行所有匹配。有很多框架内置了出色的路由,您可以使用它或构建自己的,使用 PHP 中的 explode() 函数之类的东西。

【讨论】:

  • 使用重写规则获取内部服务器错误 RewriteRule ^([a-zA-Z0-9_-\&]{3,100})/([^/]+)/([^/]+ )?$ index\.php?page=$1&s=$2&o=$3 [L] RewriteRule ^([a-zA-Z0-9_-\&]{3,100})/([^/]+)/([ a-zA-Z0-9_\&]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L] RewriteRule ^([a-zA- Z0-9_-\&]{3,100})/([^/]+)/([a-zA-Z0-9_\&]+)/([a-zA-Z0-9_\&]+)/ ([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L] RewriteRule ^([a-zA-Z0-9_-\&]{3,100} )/([^/]+)/([a-zA-Z0-9_\&]+)/([a-zA-Z0-9_\&]+)/([a-zA-Z0-9\ &]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5&r=$6 [L]
  • 是的,因为 '-' 不是列表中的最后一个字符。
【解决方案2】:

问题在于 & (amperstand) char 是 apache 的特殊字符(它分隔参数)。因此,要识别这一点,请使用 [B] 标志(在您的情况下,将其附加在 [L] 之后:

RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L,B]
RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L,B]
RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L,B]
RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5&r=$6 [L,B]

写入规则有效,第二组比匹配“s”参数:/([^/]+)/,它可以替换为/.+/。效果很好,我测试过。

基于:htaccess rewrite rule with escaped ampersand in $_GET fails

【讨论】:

  • 我用 B 标志尝试了你的规则,但它仍然给出错误的结果检查这里industrialstores.com/search_results/…
  • 发布您的 .htaccess。他们在第一行有一个“RewriteEngine On”?
  • RewriteEngine On RewriteCond %{HTTP_HOST} !^www\。 RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+)/([ ^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L,B] RewriteRule ^([a-zA-Z0-9_-]{3,100})/([^/]+ )/([a-zA-Z0-9_]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L,B] RewriteRule ^([ a-zA-Z0-9_-]{3,100})/([^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([ ^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L,B] RewriteRule ^([a-zA-Z0-9_-]{3,100})/ ([^/]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9]+)/([^ /]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5&r=$6 [L,B]
  • 我试过没有 RewriteEngine On 但还是一样
  • 不,RewriteEngine On 必须存在于 .htaccess 中。没有 RewriteCond %{HTTP_HOST} 的测试!^www\。重写规则 ^(.*)$ %{HTTP_HOST}/$1 [R=301,L]
猜你喜欢
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 2014-03-01
  • 2013-11-25
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
相关资源
最近更新 更多