Apache mod#u rewrite变量始终==';php';
我有以下文件结构:Apache mod#u rewrite变量始终==';php';,apache,mod-rewrite,Apache,Mod Rewrite,我有以下文件结构: /framework /.htaccess /index.php 以及.htaccess文件中的以下规则: <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^(.*)$ index.php?q=$1 [L] </IfModule> 重新启动发动机 重写规则^(.*)$index.php?q=$1[L] 当我导航到http://localhost/framew
/framework
/.htaccess
/index.php
以及.htaccess文件中的以下规则:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?q=$1 [L]
</IfModule>
重新启动发动机
重写规则^(.*)$index.php?q=$1[L]
当我导航到
http://localhost/framework/example
我希望查询字符串等于“framework/example”,但它等于“index.php”。为什么?当我期望变量相等时,我如何使其相等?因为您已经用重写规则
重写了url,并且已经将前面的路径放入q
。因此,只需使用$\u GET['q']
即可,因为您已经使用重写规则
重写了url,并且已经将前面的路径放入q
。所以只需使用$\u GET['q']
您的重写规则正在循环。Mod_rewrite不会停止重写,直到URI(不带查询字符串)在通过规则之前和之后都相同。当您最初请求时,会发生以下情况:
/framework/example
并去掉前导“/”框架/示例
已通过规则framework/example
被重写为index.php?q=framework/example
framework/example
!=index.php
index.php?q=framework/example
返回重写规则index.php
被重写为index.php?q=index.php
index.php
=index.php
index.php?q=index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]
你的重写规则正在循环。Mod_rewrite不会停止重写,直到URI(不带查询字符串)在通过规则之前和之后都相同。当您最初请求时,会发生以下情况:
/framework/example
并去掉前导“/”框架/示例
已通过规则framework/example
被重写为index.php?q=framework/example
framework/example
!=index.php
index.php?q=framework/example
返回重写规则index.php
被重写为index.php?q=index.php
index.php
=index.php
index.php?q=index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]
我刚刚尝试添加该规则,结果仍然相同。将RewriteCond正则表达式更改为:!index\.php得到了期望的结果,但是/framework/testing/index.php不会重定向(原因很明显)@Peter Horne:use
RewriteCond%{REQUEST_FILENAME}-f
然后。这在大多数情况下都是适用的,这是一种改进,但我刚刚意识到,RewriteCond$1^index\.php
完美地解决了这个问题。谢谢你的帮助!我刚刚尝试添加该规则,结果仍然相同。将RewriteCond正则表达式更改为:!index\.php得到了期望的结果,但是/framework/testing/index.php不会重定向(原因很明显)@Peter Horne:useRewriteCond%{REQUEST_FILENAME}-f
然后。这在大多数情况下都是适用的,这是一种改进,但我刚刚意识到,RewriteCond$1^index\.php
完美地解决了这个问题。谢谢你的帮助!我不明白你的意思,你能详细说明一下吗?原始请求中没有查询字符串,因此如果没有我添加的规则,$\u GET['q']将是空的。@Peter Horne:有什么你不能准确理解的?原始请求位于GET的q
变量中,为了避免无限重写循环,请遵循Jon Lin的回答(这是正确的,应该可以工作),我误解了,以为您在谈论传入的请求(在它到达我的服务器之前)已经设置了?q!谢谢你的帮助。我不明白你的意思,你能详细说明一下吗?原始请求中没有查询字符串,因此如果没有我添加的规则,$\u GET['q']将是空的。@Peter Horne:有什么你不能准确理解的?原始请求位于GET的q
变量中,为了避免无限重写循环,请遵循Jon Lin的回答(这是正确的,应该可以工作),我误解了,以为您在谈论传入的请求(在它到达我的服务器之前)已经设置了?q!谢谢你的帮助。