总是mod_rewrite的变量=='的index.php' [英] mod_rewrite variable always == 'index.php'

查看:103
本文介绍了总是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:


  1. 重写引擎将 /框架/例子键,去掉开头的/

  2. 框架/例子通过规则放在

  3. 框架/例子被改写为的index.php?Q =框架/例子

  4. Rerite引擎比较之前和之后,框架/例子!= 的index.php

  5. 的index.php?Q =框架/例子追溯到通过重写规则

  6. 的index.php 被改写为的index.php?Q =的index.php

  7. 重写引擎比较之前和之后,的index.php == 的index.php

  8. 重写引擎停止,由此产生的URI是的index.php?Q =的index.php

  1. Rewrite engine takes /framework/example and strips the leading "/"
  2. framework/example is put through the rules
  3. framework/example gets rewritten to index.php?q=framework/example
  4. Rerite engine compares the before and after, framework/example != index.php
  5. index.php?q=framework/example goes back through the rewrite rules
  6. index.php gets rewritten to index.php?q=index.php
  7. Rewrite engine compares the before and after, index.php == index.php
  8. 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]

这篇关于总是mod_rewrite的变量=='的index.php'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆