2012-04-20 120 views
4

我正在構建一個php框架,將所有流量重定向到ROOT/public/index.php,然後將url放入get請求中。我的問題是我的RewriteConds不能正常工作,正在接受文件和文件夾名稱。Mod_rewrite - 重寫條件不起作用

在根目錄

<IfModule mod_rewrite.c> 

RewriteEngine on 
RewriteRule ^$ public/ [L] 
RewriteRule (.*) public/$1 [L] 

</IfModule> 

<files .htaccess> 

order allow,deny 
deny from all 

</files> 

在公共目錄

<IfModule mod_rewrite.c> 

    RewriteEngine On 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 

</IfModule> 
+0

快速的問題是ü運行它在http://本地主機/或你運行它在DS – kyle1046 2012-04-20 20:18:05

+0

在localhost – 2012-04-21 00:40:25

+0

沒有運行if語句刪除,浪費資源而不是必需的。 – JREAM 2012-04-21 02:24:49

回答

1

[L]標誌告訴Apache在規則匹配後停止重定向。因此,假設您的ROOT文件夾首先出現,它將運行並停止在重定向,這將導致第二次重定向無用。

要解決的問題有這樣的:

RewriteEngine on 
RewriteRule ^$ public/ 
RewriteRule (.*) public/$1 
+0

非常感謝!像魅力一樣工作。 – 2012-04-23 03:41:14

0

如果你想,即使現有的文件和目錄的腳本重定向:在公共目錄

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteRule ^(.*)$ index.php?url=$1 [PT,L] 
</IfModule> 

我的2分提示:刪除?url$1部分,看看$_SERVER['REQUEST_URI']中有什麼。

+0

如果我拿出這些行,那麼'$ _GET ['url']'將永遠是index.php,這不是我想要的。我猜這就是爲什麼你建議'$ _SERVER ['REQUEST_URI']'。感謝您的回答,但不是我要找的。 – 2012-04-23 05:30:48