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


Removing an element anywhere in URL by htaccess

首先:我过去有一个错误的策略,在我的url中包含无用/无意义的"seo_url=keyword"元素,现在我想在url中的任何位置删除它,并在没有该元素的情况下重写我的url。(301重定向)

第二:有时一个元素或一些元素是空的,比如"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>

Ad 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]

必须向目标URL添加查询字符串。否则,查询字符串将被原封不动地追加。