如何从 apache 重写规则中的特定 $_GET 变量中排除匹配值?

How do I exclude matched values from a particular $_GET variable in an apache rewrite rule?

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

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

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 重写。我希望这是我犯的一个小错误。谁能提供一些帮助?非常感谢。

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

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