htaccess rewrite querystring and remove empty valu

2019-09-01 07:41发布

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条回答
狗以群分
2楼-- · 2019-09-01 08:15

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.

查看更多
登录 后发表回答