通过htaccess删除URL中任何位置的元素

时间:2015-12-19 20:39:32

标签: php .htaccess mod-rewrite url-rewriting

首先:过去我的策略是错误的,包括无用/废话" seo_url = keyword"我的网址中的元素,现在我想删除它在URL中的任何位置,并重写我的网址没有该元素。 (301重定向)

第二:有时一个元素或一些元素是空的,如#34; location ="或" location =& select ="我会删除这些元素,如果它们在URL中的任何位置都是空的,并重写我的URL而不使用这些元素。 (301重定向)

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

1 个答案:

答案 0 :(得分:0)

广告1)要删除查询字符串中的seo_url=keyword,请使用RewriteCond提取要保留的部分

RewriteCond &%{QUERY_STRING}& (.*?)&seo_url=.*?&(.*)
RewriteRule ^ %{REQUEST_URI}?%1&%2 [R,L]

Ad 2)从查询字符串中删除所有空元素的工作方式相同

RewriteCond &%{QUERY_STRING}& (.*?)&.*?=&(.*)
RewriteRule ^ %{REQUEST_URI}?%1&%2 [L]

您必须将查询字符串添加到目标网址。如果不是,查询字符串将不加改变地附加。