【问题标题】:Rewrite redirects the simple regex to base file重写将简单的正则表达式重定向到基本文件
【发布时间】:2011-03-08 18:19:45
【问题描述】:

我正在尝试执行基本的 mod_rewrite 清理 URL 技巧,其中 /category/item 重写为 /category/index.php?id=item。在/category 目录中,我的 .htaccess 文件中有这个:

Options +FollowSymLinks
RewriteEngine  on
RewriteBase  /category/
RewriteRule  ^(.+)$  index.php?id=$1  [L]

它将请求发送到index.php 脚本就好了,但是我没有在 id 变量中找到“item”,而是得到了“index.php”。也就是/category/item好像改写成/category/index.php?id=index.php了。

我尝试了无穷无尽的变化,有不同/没有 RewriteBase 和其他更改,但没有任何效果。知道我哪里出错了吗?

【问题讨论】:

  • 当你说“在 id 变量中”时,你的意思是$_GET["id"]吗?
  • 是的,完全正确。 $_GET 的 print_r 显示 Array ( [id] => index.php )

标签: regex .htaccess mod-rewrite apache2


【解决方案1】:

问题是文件 index.php 与 htaccess 位于同一目录中,并且由重写包含和处理。尝试添加重写条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

此外,您传递给 id 参数的输入非常不安全。假设您使用数字 id,您可能希望使用 [0-9] 而不是 . 以确保仅传递数字。如果它们是字母数字,您仍然希望使用类似:[a-zA-Z0-9\-_]

【讨论】:

    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2013-07-25
    • 2023-03-14
    • 2020-09-07
    相关资源
    最近更新 更多