Issue with urlencode


Issue with urlencode

我在过滤系统中有一个编码&符号的问题。
例如,如果第一个过滤器是针对品牌的,而品牌名称是"Head &肩部,url应该看起来像网站/类别/品牌头+%26+肩膀,但过滤器只有当url是网站/类别/品牌头-%252526+肩膀时才有效。
如果选择了另一个过滤器,例如高度,品牌过滤器工作,如果url是网站/类别/品牌头-%25252526+肩膀等,我必须在百分比符号之前添加另一个25。

这是品牌过滤器代码

if($brand == false){
    $data['brands'] = array();
    $brands         = $attributes->getBrands($av_list, $_GET['c']);
    $brandTotalCnt  = 0;
    if(!empty($brands)){
        foreach ($brands as &$brand) {
            if($av_list){
                $cur_av_list  = $av_list  . $brand['brand'];
                $cur_av_uri   = $_GET['av'] . $brand['brand'];
            } else {
                $cur_av_list  = $brand['brand'];
                $cur_av_uri   = $brand['brand'];
            }
            $tmp_uri          = explode('/', $ln_uri);
            $tmp_uri_array    = $attributes->remove_items_by_value($tmp_uri, 'brand');
            $new_uri          = implode('/', $tmp_uri_array);   
            $brandTotalCnt   += $brand['num'];
            $data['brands'][] = array(
                    'brand'           => ucwords(strtolower($brand['brand'])),
                    'num'             => $brand['num'],
                    'href'            => $url->link('/c/'.$_GET['c'], '/brand-'. urlencode(strtolower($brand['brand'].'1')). $new_uri)
                );
        }
    }
    $data['brandTotalCnt'] = $brandTotalCnt;
}

每个过滤器的Urldecode

if(isset($brand)){
    $sql .= " AND brand = '".urldecode($brand)."'";
}
if(isset($gramaj)){
    $height= $_GET['height'];
    $sql .= " AND height= '".urldecode($height)."'";
}

这是来自我的htaccess

# rewrite /category/brand-mybrand/country-mycountry/offer-yes/new-yes
# to /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^c/([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA]
# rewrite /index.php/brand-mybrand/country-mycountry/offer-yes/new-yes?c=category
# converts any /name-val/ to query parameter name=val in every rewrite
# stopping when there is no part left after /index.php
RewriteRule ^(index'.php)/([^-]+)-([^/]+)(/.*)?$ /$1$4?$2=$3 [L,QSA]

PHP在启动时自动解析查询字符串和url解码值,当它填充$_GET[]时。

当你生成一个URL时,urlencode()的值是正确的,但你不能从$_GET[]中选择urldecode()的值。

更新:

正如Apache文档中解释的那样:

mod_rewrite必须在映射url之前对它们进行转义,因此反向引用在应用

时是未转义的。

如果您的重写规则使用路径的一部分并将其放入查询字符串中,则标志[B](转义反向引用)指示引擎使用未转义的URL来匹配规则。

你的问题有两个解决方案:

  1. (简单的解决方案):生成url到而不是包含非字母数字字符;只使用字母,数字和破折号(-),你是安全的;
  2. (高级解决方案):在受影响的重写规则中添加[B]标志:

    RewriteRule ^c/([^/]+)/([^-]+.+)/?$ /index.php/$2?c=$1 [L,QSA,B]