2011-04-16 48 views
0

我已經有一些麻煩以下工作:國防部改寫成子目錄

<IfModule mod_rewrite.c> 
    DirectoryIndex index.php 

    Options +FollowSymLinks 

    RewriteEngine On 

    # If the file is not found in web 

    RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem. 
    RewriteRule ^(.*)$ web/$1 [L] 

    # Then rewrite it to index.php 

    RewriteRule ^(.*)$ index.php [L,QSA] 
</IfModule> 

的想法是,以檢查文件中的web目錄存在,如果沒有,將請求路由到index.php。

上面的問題行似乎拿起了正確的URL,但它不能識別該文件存在。

例如:

http://localhost/img.gif --> /www/web/img.gif 
http://localhost/subdir/file.doc --> /www/web/subdir/file.doc 
http://localhost/user/add --> /www/index.php 
http://localhost/invalidurl --> /www/index.php 

但是,我不能讓被服務的靜態資源正確,他們都路由到index.php文件。

我也想保留所有的URL相對;所以我可以在不編輯它的情況下使此代碼可重用。

下面的.htaccess給出了一個內部服務器錯誤,如果我訪問img.gif:

<IfModule mod_rewrite.c> 
    DirectoryIndex index.php 

    Options +FollowSymLinks 

    RewriteEngine On 

    # If the file is not found in web 

    #RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem. 
    RewriteRule ^(.*)$ web/$1 [L] 

    # Then rewrite it to index.php 

    #RewriteRule ^(.*)$ index.php [L,QSA] 
</IfModule> 

這的.htaccess重定向到http://localhost/C:/absolute/path/to/web/img.gif,參觀img.gif時:

<IfModule mod_rewrite.c> 
    DirectoryIndex index.php 

    Options +FollowSymLinks 

    RewriteEngine On 

    # If the file is not found in web 

    #RewriteCond web/$1 -f [NC] #<-- This line seems to be the problem. 
    RewriteRule ^(.*)$ web/$1 [R] 

    # Then rewrite it to index.php 

    #RewriteRule ^(.*)$ index.php [L,QSA] 
</IfModule> 

我的結論從這將是它正在獲得路徑正確,但一些奇怪的是導致它做一些奇怪的事情(我甚至不知道它爲什麼有一個內部服務器錯誤 - 應該404找不到)。

+0

請給我一些建議。 – xaav 2011-04-16 22:09:41

+0

哇。這是我第一次沒有得到答案。 – xaav 2011-04-18 01:39:43

+0

「靜態資源」是什麼意思? – 2011-04-18 10:24:45

回答

1

好吧我知道了:

當你做了改寫,那麼請記住,重寫的URL在內部調用。 所以你基本上必須重新處理大多數不同值的.htaccess。

所以在你的例子中:http://localhost/img.gif被定向到http://localhost/web/img.gif然後到http://localhost/index.php的最後一條規則。

我會嘗試這(通過替換最後一個規則:)

RewriteCond %{SCRIPT_NAME} !^/web/ 
RewriteRule ^(.*)$ index.php [L] 

(注:[QSA]沒有必要的,因爲你不碰查詢字符串,所以它傳遞的是)

編輯:那

# If the file is not found in web 
RewriteCond web/$1 !-f [NC] 
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php 
RewriteCond %{SCRIPT_NAME} !^/web/ 
#Rewrite it to index.php 
RewriteRule ^(.*)$ index.php [R,L] 

# If the file is found in web 
RewriteCond web/$1 -f [NC] 
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php 
RewriteCond %{SCRIPT_NAME} !^/web/  
#Then point to web/image.gif 
RewriteRule ^(.*)$ web/$1 [L] 
+0

謝謝。既然你是唯一的答案,即使它沒有實際工作,我也是正確的。 – xaav 2011-04-18 23:03:51

+0

嗯,它沒有工作,但謝謝你的嘗試。 – xaav 2011-04-18 23:11:30

+0

那麼如果它不工作,我們應該嘗試着去做。 – 2011-04-18 23:33:13