【问题标题】:htaccess rewrite querystring and remove empty valuehtaccess 重写查询字符串并删除空值
【发布时间】:2012-05-25 16:29:39
【问题描述】:

首先,对不起我的英语不好。

我尝试重写从 Form Get 生成的 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 中删除生成的双斜杠?

谢谢

【问题讨论】:

    标签: .htaccess query-string


    【解决方案1】:

    最简单的方法是进行另一个重定向(不是很漂亮,因为它需要两个 301)。

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

    有趣的是,当加载的 url 带有双斜杠时,mod_rewrite 会自动删除它。因此,正如您在上面看到的,您只需将 url 重写为自身,有点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-11
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多