【问题标题】:How do I exclude matched values from a particular $_GET variable in an apache rewrite rule?如何从 apache 重写规则中的特定 $_GET 变量中排除匹配值?
【发布时间】:2016-03-11 15:22:59
【问题描述】:

我试图在 apache 重写规则中省略 "?main_page=" 的 $_GET 变量的某些值。例如:

我有一些网址要重写,比如:

http://example.com/index.php?main_page=faq
http://example.com/index.php?main_page=login
http://example.com/index.php?main_page=contact_us

变成:

http://example.com/faq
http://example.com/login
http://example.com/contact_us

我能够做到这一点:

  RewriteCond %{QUERY_STRING} main_page=(.*)
  RewriteRule ^index\.php$ %1? [R=302,L]

不过,我也想从重写规则中省略某些“?main_page=”的值,例如:

http://example.com/index.php?main_page=index    
http://example.com/index.php?main_page=page&id=2
http://example.com/index.php?main_page=advanced_search&search_in_description=1&keyword=cialis&inc_subcat=0
http://example.com/index.php?main_page=product_info&cPath=1&products_id=27

所以我尝试了类似的方法:

  RewriteEngine on
  Options -MultiViews
  RewriteCond %{QUERY_STRING} main_page=(?!product_info)(?!page)(?!index)(?!advanced_search)
  RewriteRule ^index\.php$ %1? [R=302,L]

但没有运气。我不擅长 regx 重写。我希望这是我犯的一个小错误。有人可以提供一些帮助吗?非常感谢。

【问题讨论】:

    标签: regex apache .htaccess rewrite


    【解决方案1】:

    要否定多个键值,您可以使用:

      RewriteCond %{QUERY_STRING} main_page=((?!index|product_info|page|foo|bar).*)
      RewriteRule ^index\.php$ %1? [R=302,L]
    

    【讨论】:

    • 谢谢,效果很好!我今天学到了一些新东西;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 2022-06-10
    • 2015-08-03
    相关资源
    最近更新 更多