htaccess rewrite querystring and remove empty value

陌路散爱 提交于 2019-12-13 03:55:21

问题


first, sorry for my bad English.

I try to rewrite url generated from Form Get and redirect that.

my url is like this:

http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city

and I have this .htaccess configured:

11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]

So basically all my request are direct to index.php.

21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]

All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:

http://www.mysite.com/for-rent/my-category//my-zone/my-city/

The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?

Thanks


回答1:


Easiest is to do another redirect (not real pretty as it requires two 301's).

RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]

The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.



来源:https://stackoverflow.com/questions/10758138/htaccess-rewrite-querystring-and-remove-empty-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!