Apache mod_rewrite:仅当没有页面请求或 index.php WITHOUT query

Apache mod_rewrite: only if no page requested or index.php WITHOUT query

如何使用mod_rewrite重写一个URL如果提出以下简单的要求

/
/index.php

但不适用于带有查询字符串的请求

/?query=vars    
/index.php?query=vars

我试过以下方法

DirectoryIndex pages.php?id=1 index.php

RewriteEngine on
RewriteRule ^index.php$ /pages.php?id=1

我正在寻找一种解决方案,其中 mysite.commysite.com/index.php 会按书面显示但实际上请求 mysite.com/pages.php?id=1

但是 mysite.com/index.php?query=vars 不受影响

您可以添加一个条件来检查查询字符串:

DirectoryIndex pages.php?id=1 index.php

RewriteEngine on

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(index\.php)?$ pages.php?id=1 [L,NC]
  1. RewriteCond %{QUERY_STRING} ^$ 将确保仅在查询字符串为空时触发此规则。
  2. ^(index\.php)?$ 将使此规则针对 /index.php/(着陆页)触发。