English 中文(简体)
htaccess重写查询字符串并删除空值
原标题:htaccess rewrite querystring and remove empty value

首先,很抱歉我的英语不好。

我尝试重写从Form Get生成的url并重定向。

我的url是这样的:

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. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]

所以基本上我所有的请求都直接指向index.php。

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

所有这些都可以,但问题是,当我在查询字符串中有一个空值时,规则会添加双斜线,上面的url(例如whit&;type=&;zone=my zone…type有空值)会这样翻译:

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

问题是:如果查询字符串中有一个或多个空值,我如何在.htaccess中删除生成的双斜杠?

谢谢

最佳回答

最简单的是做另一个重定向(不是真正的漂亮,因为它需要两个301)。

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

有趣的是,当url中加载了双斜杠时,mod_rewrite会自动删除它。正如你在上面看到的,你只需要将网址重写为它自己,有点。

问题回答

暂无回答




相关问题
How can I make a query string tamper-proof?

I need to use query strings in a URL, but need to ensure that they haven t been tampered with. I found a solution which almost works, but the encoded strings get mangled by a service my app needs to ...

Passing Lists as a QueryString - should I use JSON

Lets say I ve got an online dating site, users can filter the user list based on various criteria, Height, Age, BodyType, Ethnic Origin.... I want to pass the criteria to the pager, via QueryString. ...

Is using URL query string variables still RESTful

I m presented with a problem of using query string variables in a RESTful Asp.net MVC application. Does that violate the RESTful pattern? The thing is that Asp.net MVC uses default route like: /...

URL requests adding POST data to querystring

I am using ExtJS to send an Ajax request to a PHP page on a server, wanting to send the parameters as POST variables rather than in the querystring. I have included a random token in the querystring ...

Maintain the querystring in all pages in mvc

I need to maintain the querystring in all pages in my asp.net mvc(C#) application. For ex.: I will call a page www.example.com?Preview=True. The querystring should be maintained whatever the page i ...

Nginx 301 redirect inc. set cookie

what I m looking for is the ability for nginx to detect that a url has the query string variable cid. for example www.adomain.com/froggy?cid=12&as=false (query string can be solo or mixed with ...

ASP.NET: URI handling

I m writing a method which, let s say, given 1 and hello should return http://something.com/?something=1&hello=en. I could hack this together pretty easily, but what abstraction functionality ...

Checking querystring values in PHP

http://localhost/?area=characters&name=Michal+Stroganof $result = mysql_query("SELECT * from players WHERE name = $_GET[name] "); while ($row = mysql_fetch_assoc($result)) { echo "Name: " ....

热门标签