重写将简单的正则表达式重定向到基本文件

发布于 2024-09-07 08:21:11 字数 530 浏览 0 评论 0原文

我正在尝试执行基本的 mod_rewrite clean URL 技巧,其中 /category/item 重写为 /category/index.php?id=item。在 /category 目录中,我的 .htaccess 文件中包含以下内容:

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

它将请求发送到 index.php 脚本,但不是在中查找“item” id 变量,我得到“index.php”。也就是说,/category/item似乎被重写为/category/index.php?id=index.php

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

I'm trying to do the basic mod_rewrite clean URL trick, where /category/item rewrites to /category/index.php?id=item. In the /category directory, I have this in my .htaccess file:

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

It sends the request to the index.php script just fine, but instead of finding "item" in the id variable, I'm getting "index.php". That is, /category/item seems to be rewriting to /category/index.php?id=index.php.

I've tried endless variations, with different/no RewriteBase and other changes, but nothing is working. Any idea where I've gone wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2024-09-14 08:21:11

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

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

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

The problem is the file index.php is in the same directory as htaccess and is included and handled by the rewrite. Try adding rewrite conditions:

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

Also, the input you are passing to the id paramater is very insecure. Assuming you use numeric ids, you may want to use [0-9] instead of a . to ensure only numbers are passed. If they are alphanumeric, you would still want to use something like: [a-zA-Z0-9\-_].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文