apache - 使用否定条件重写条件

标签 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

最佳答案

我找到了答案。

错误是试图获取 GET参数与 REQUEST_URI .正确的用法应该是 QUERY STRING像这样:

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

关于apache - 使用否定条件重写条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16882838/

相关文章:

PHP session 超时为 0,但 session 仍将过期

html - 如何将所有 url 重写为 "/"?

.htaccess - 使用 .htaccess 强制 HTTPS 导致重定向循环

java - Excel POI API 更改单元格值

php - 我怎样才能强制从 : header (envelope sender) in PHP without putting it in mail()?

linux - htaccess 规则不适用于 linux 服务器

apache - 文件中断的 mod_rewrite 和 htaccess 路径

.htaccess - 当 '/folder/index.html' 被剥离时,如何删除 'index.html' 的尾部斜杠?

php - 如何重定向 Apache "Index of ..."页面?

php - 用htaccess密码保护识别 "apple-touch-icon"