2011-12-01 37 views
0

我有以下文件結構:mod_rewrite的變量總是== '的index.php'

/framework 
    /.htaccess 
    /index.php 

,並在我的.htaccess文件下列規則:

<IfModule mod_rewrite.c> 

    RewriteEngine on 
    RewriteRule ^(.*)$ index.php?q=$1 [L] 

</IfModule> 

當我瀏覽到http://localhost/framework/example我會期望查詢字符串等於'框架/示例',而是等於'index.php'。爲什麼?當我期待它的時候,如何讓變量平等?

回答

3

您的重寫規則正在循環。 Mod_rewrite不會停止重寫,直到URI(不含查詢字符串)在遍歷規則前後都是相同的。當您最初要求http://localhost/framework/example這是發生了什麼:

  1. 重寫引擎需要/framework/example和鋼帶領先「/」
  2. framework/example通過規則
  3. framework/example被改寫爲index.php?q=framework/example
  4. Rerite引擎比較之前和之後,framework/example!= index.php
  5. index.php?q=framework/example追溯通過重寫規則
  6. index.php被改寫爲index.php?q=index.php
  7. 重寫引擎比較之前和之後,index.php == index.php
  8. 重寫引擎停止後,得到的URI是index.php?q=index.php

您需要添加一個條件,使其將不會重寫兩次相同的URI:

RewriteEngine on 
RewriteCond %{REQUEST_URI} !^/index\.php 
RewriteRule ^(.*)$ index.php?q=$1 [L] 
+0

爲什麼低調?完全有效的答案。 +1 – zerkms

+0

我剛剛嘗試添加該規則,結果仍然相同。 –

+0

將RewriteCond正則表達式更改爲:!index \ .php獲取所需結果,但/framework/testing/index.php不會重定向(出於顯而易見的原因) –

3

因爲你改寫RewriteRule的網址,並已以前的路徑q。所以只需使用$_GET['q']

+0

我不明白你的意思,你可以擴展一下嗎?原始請求中沒有查詢字符串,因此如果沒有我添加的規則,那麼$ _GET ['q']將是空的。 –

+0

@Peter Horne:你不明白什麼?原始請求在GET的'q'變量中,並且爲了避免無限重寫循環 - 遵循Jon Lin的回答(這是正確的並且應該起作用) – zerkms

+0

我誤解了,並且認爲你在談論傳入請求(在它到達我的服務器之前)有?q =已經設置!謝謝你的幫助。 –