2011-12-04 31 views
7

我正在使用mod_rewrite將所有請求路由到index.php。。使用別名URI時忽略.htaccess

我的文件夾結構如下:

/Users/Peter/Projects/Framework 
    /.htaccess 
    /index.php 

在我的.htaccess文件我有以下幾點:

RewriteEngine on 
RewriteCond $1 !^index\.php/ 
RewriteRule ^(.*)$ index.php/$1 [L] 

當我訪問projects.localhost/framework/example這工作得很好。

我也有以下別名設置: 別名/〜別名/用戶/彼得/項目

當我瀏覽到http://projects.localhost/~alias/framework/example我得到一個404錯誤,在我的錯誤日誌中的:File does not exist: /Users/Peter/Projects/framework/example

當我使用別名URL時,似乎沒有調用.htaccess文件(在使用別名URL時,輸入giberish到.htaccess文件不會觸發任何類型的錯誤似乎證實了這一點)。

AllowOveride設置爲All。

如何在使用別名URL時使.htaccess工作,並且無論URL如何(別名或不別名)都重寫規則一致地應用?

編輯:導航到projects.localhost/~alias/framework/index.php/example也工作正常,確認別名工作正常(除了沒有應用.htaccess規則)。

+1

別名似乎繞過正常的根,因此.htaccess文件放在那裏。你可以在'/ Users/Peter/Projects'文件夾中加入另一個.htaccess文件 – Gerben

+0

我已經嘗試在項目文件夾中放置一個.htaccess文件,但這對別名URL也沒有影響。 (它確實會影響不帶別名的url。) –

回答

0

你確定你已經啓用了mod_rewrite嗎?

+0

是的,當我使用不帶別名的URL時,重寫工作正常 –

8

我發現你的問題有同樣的問題。從@Gerbens發表評論我設法找到Apache手冊其中規定this part

# 
# /abc/def/.htaccess -- per-dir config file for directory /abc/def 
# Remember: /abc/def is the physical path of /xyz, i.e., the server 
#   has a 'Alias /xyz /abc/def' directive e.g. 
# 

RewriteEngine On 

# let the server know that we were reached via /xyz and not 
# via the physical path prefix /abc/def 
RewriteBase /xyz 

# now the rewriting rules 
RewriteRule ^oldstuff\.html$ newstuff.html 

在別名目錄添加重寫基地.htaccess文件解決了我的問題。

所以你的情況

RewriteEngine on 
RewriteBase /~alias 
RewriteCond $1 !^index\.php/ 
RewriteRule ^(.*)$ index.php/$1 [L] 
+0

如果可以的話,我會投票兩次 - 保存我的培根!謝謝,@danneth。 –

+0

太好了,謝謝。因此,如果沒有RewriteBase,Apache通過假定從物理路徑訪問它會表現得很愚蠢。 我想知道這種行爲沒有被報告爲一個錯誤。 –