2012-07-05 31 views
0

我正在處理基於CodeIgniter的應用程序。我htacess看起來是這樣的:htaccess重寫不工作,因爲它應該與www

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteBase/
RewriteRule ^(.*)$ /index.php?$1 [L] 
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC] 
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] 

這在大多數情況下的偉大工程,我可以訪問控制器和方法是這樣的:但是

http://example.com/controller/method/args 

,如果附加一個WWW,這樣

http://www.example.com/foo 

它重定向到

http://example.com/index.php?foo 

顯示的頁面始終是我網站的索引頁面。

我添加了最後一對RewriteCond/RewriteRule,只是爲了刪除www(如果存在)。

我不太知道如何得到它的工作就是我想要的,讓

http://www.example.com/foo 

只是重定向到

http://example.com/foo 

編輯

從我的CI配置文件:

$config['base_url'] = 'http://example.com/'; 
$config['index_page'] = ''; 
$config['uri_protocol'] = 'AUTO'; 
+0

你有沒有設置'$配置[「index_page」]''中的應用/配置/ config.php'? – 2012-07-05 20:12:52

+0

是的。它被設置爲'$ config ['index_page'] ='';' – xbonez 2012-07-05 20:13:46

+0

投票決定關閉它的人可以解釋原因嗎? – xbonez 2012-07-05 20:14:47

回答

3

正如評論中所述。

您的重寫規則RewriteRule ^(.*)$ /index.php?$1 [L]應放置在底部。在這種情況下,由於您的[L]標誌,訂單很重要。

1

沒有真正提供比史蒂芬路的任何更多...但清理你爲了讓更多一點意義

RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC] 
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
# You don't really need the !-d (directory) flag here 
RewriteRule ^(.*)$ /index.php [L] # Could put QSA in here too, but CI doesn't play too nice with query strings