重写将简单的正则表达式重定向到基本文件
我正在尝试执行基本的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是文件index.php 与htaccess 位于同一目录中,并由重写包含和处理。尝试添加重写条件:
此外,您传递给 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:
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\-_]
.