RewriteCond负面条件

时间:2013-06-02 12:38:57

标签: apache .htaccess mod-rewrite redirect rewrite

我正在尝试重写所有www.site.com/hello to www.site.com/index.php?p=hello 它适用于以下代码(在.htaccess中):

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ /index.php?p=$1

但我希望旧链接保持正常运行,以便www.site.com/?p=hello保持www.site.com/?p=hello

我已尝试过以下代码,但无效

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\?p=)
RewriteRule ^([^/]*)/?$ /index.php?p=$1

1 个答案:

答案 0 :(得分:5)

我找到了答案。

错误是尝试使用GET获取REQUEST_URI参数。正确的用法应该是QUERY STRING,如下所示:

RewriteCond %{QUERY_STRING} !(p=.*)$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ /index.php?p=$1