从URL代码中的任何位置删除任何一个或多个空参数时出错

时间:2016-01-13 21:58:05

标签: regex apache .htaccess mod-rewrite

此代码正在删除URL中任何位置的空元素或元素,但它会将我的清洁视为脏看的URL,例如从http://www.example.com/US/FL/-show.htmlhttp://www.example.com/search-products.php?productLocation=FL&countrySelect=US&submit=1

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,L,NE]

通过.php设置的干净网址是:

<select name="country" id="country" onchange="document.forms[0].action='<?php
echo $submit == 1 ? '/search.php' : '/index.php' ; ?>';document.forms[0].submit();return false;">

完整.htaccess:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example\.com
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]


RewriteCond %{QUERY_STRING} ^(.+?&)?[^=]+=(?:&(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,NE,L]


    RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
    RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
    RewriteCond %{REQUEST_FILENAME} -l [NC]
    RewriteRule .* - [L]




    RewriteRule (.*)/$ /?countrySelect=$1
    RewriteRule (.*)/+/(.*)-(.*)\.html$  search-products.php?countrySelect=$1&productSelect=$2&submit=1
    RewriteRule (.*)/tag/(.*) search-products.php?countrySelect=$1&keyword=$2
    RewriteRule (.*)/(.*)/(.*)-(.*)\.html$  search-products.php?productLocation=$2&countrySelect=$1&productSelect=$3&submit=1
    RewriteRule (.*)/(.*)_(.*)\.html$  detail.php?name=$2&id=$3&detail=true&countrySelect=$1


</IfModule>



# Protect the htaccess file
<Files .htaccess>
    Order Allow,Deny
    Deny from all
</Files>

# Protect json
<Files ~ \.json$>
    Order Allow,Deny
    Deny from all
</Files>

# Disable directory browsing
Options All -Indexes

1 个答案:

答案 0 :(得分:1)

尝试将空参数删除规则更改为:

RewriteCond %{THE_REQUEST} \?(.+?&)?[^=]+=(?:&(.*))?\sHTTP
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,NE,L]

在新浏览器中测试。