htaccess的重写查询字符串,并删除空值 [英] htaccess rewrite querystring and remove empty value
问题描述
首先,对不起,我的英语不好。
我尝试重写从表单生成的URL获取和重定向。
我的网址是这样的:
<$p$p><$c$c>http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city和我有这样的.htaccess配置:
11。的RewriteCond%{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12.重写规则(。*)%{REQUEST_URI} / 1%/ 2%/ 3%/ 4%/ 5%/? [R = 301,L]
所以基本上我所有的要求都直接指向index.php。
21。 !的RewriteCond%{REQUEST_URI}指数\ .PHP |资源|隐藏
22.重写规则^(。*)$的index.php / $ 1 [L]
所有的作品,但问题是,当我在查询字符串为空值,规则添加双斜线和上面的网址(例如丝毫&放大器;类型=放大器,带=我区...类型有空值)将翻译这样的:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
现在的问题是:如何删除在.htaccess中产生,如果我有一个或查询字符串中更多的空值双斜杠
感谢
最简单的就是做一套重定向(不是真正的pretty的,因为它需要两个301的)。
的RewriteCond%{THE_REQUEST} //
重写规则。* $ 0 R = 301,L]
有趣的部分是,当URL被加载在它双斜杠,mod_rewrite的将自动删除此。因此,大家可以在上面看到你就得改写样的网址本身。
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
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.
这篇关于htaccess的重写查询字符串,并删除空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!