[英]Why won't this simple RewriteRule work?
这应该很简单,但我无法弄清楚。 我有这个非常简单的mod_rewrite规则,它只是不想工作。 这是.htaccess文件的全部内容。
RewriteEngine On
RewriteRule ^([A-Za-z0-9-_/\.]+)/?$ index.php?page=$1 [L]
如果我调用URL domain.com/foo,则应将其重写为index.php?page = foo。 而是将其重写为index.php?page = index.php。 我尝试了多个URL:
在所有情况下,PHP都将'page'设置为“ index.php”。 这不是index.php的问题,因为我用简单的脚本替换了index.php的全部内容,但该脚本仍简单地回显了'page'的值,但仍以index.php的形式出现。
我真的错在这里错了,任何帮助都太好了!
谢谢
阿德里安
不能使用任何简单原因的任何原因,例如:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
如果您试图防止现有页面被重写,则!-f
将解决这一问题( if file does not exist
而if directory does not exist
,则re-write
)
我认为这是您想要的。 不幸的是,如果URL具有其他GET参数(IE,/ some / url / path?page = 1),则不能很好地工作。
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1 [L]
更好的主意。
RewriteEngine On
RewriteRule ^(.*)$ index.php [L,QSA]
QSA标志将转发GET参数,然后在index.php中使用$_SERVER['REQUEST_URI']
和parse_url
路由请求。
从http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html :
'qsappend|QSA' (query string append)
This flag forces the rewriting engine to append a query string part
in the substitution string to the existing one instead of replacing it.
Use this when you want to add more data to the query string via a rewrite rule.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.