总是mod_rewrite的变量=='的index.php'
我有以下文件结构:
/framework
/.htaccess
/index.php
在我的.htaccess文件以下规则:
and the following rules in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?q=$1 [L]
</IfModule>
当我浏览到的http://本地主机/框架/例子
我希望查询字符串来平等的框架/例如',而是它等于'的index.php 。为什么?我如何获得变量时,我就期待等于?
When I navigate to http://localhost/framework/example
I would expect the query string to equal 'framework/example' but instead it equals 'index.php'. Why? And how do I get the variable to equal when I'm expecting it to?
您重写规则的循环。 mod_rewrite的不会停止改写直到该URI(没有查询字符串)是一样的不用通过规则之前和之后。当您最初要求的http://本地主机/框架/例如这是发生了什么:
Your rewrite rules are looping. Mod_rewrite won't stop rewriting until the URI (without the query string) is the same before and after it goes through the rules. When you originally request http://localhost/framework/example this is what happens:
- 重写引擎将
/框架/例子
键,去掉开头的/ -
框架/例子
通过规则放在 -
框架/例子
被改写为的index.php?Q =框架/例子
- Rerite引擎比较之前和之后,
框架/例子
!=的index.php
-
的index.php?Q =框架/例子
追溯到通过重写规则 -
的index.php
被改写为的index.php?Q =的index.php
- 重写引擎比较之前和之后,
的index.php
==的index.php
- 重写引擎停止,由此产生的URI是
的index.php?Q =的index.php
- Rewrite engine takes
/framework/example
and strips the leading "/" -
framework/example
is put through the rules -
framework/example
gets rewritten toindex.php?q=framework/example
- Rerite engine compares the before and after,
framework/example
!=index.php
-
index.php?q=framework/example
goes back through the rewrite rules -
index.php
gets rewritten toindex.php?q=index.php
- Rewrite engine compares the before and after,
index.php
==index.php
- Rewrite engine stops, the resulting URI is
index.php?q=index.php
您需要添加一个条件,使它不会重写相同的URI两次:
You need to add a condition so that it won't rewrite the same URI twice:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]