2012-12-03 45 views
1

所以我玩.htaccess在我的codeigniter應用程序有乾淨的網址。htaccess問題導致內部服務器錯誤太多重定向

總之

,我試圖:

1)除去的index.php在網址(重定向永久)

http://localhost/directory/index.php* 
to 
http://localhost/directory/* 

http://my.domain.com/index.php* 
to 
http://my.domain.com/* 

2)重寫請求某些控制器到index.php/[ CONTROLLER_NAME]

http://localhost/directory/controller1* 
to 
http://localhost/directory/index.php/controller1* 

http://my.domain.com/controller2* 
to 
http://my.domain.com/index.php/controller2* 

我htaccess文件目前是這樣的:

Options +FollowSymlinks 

RewriteEngine On 
#RewriteBase/

# Redirect index.php 
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] 
RewriteCond %{THE_REQUEST} !/system/.* 
RewriteRule ^(.*)index\.php((/)(.*))?$ /$1/$4 [R=301,L] 

第一個問題

,這並不爲http://localhost/dir/index.php/controller1工作。 不是重定向到http://localhost/dir/controller1,重定向到http://localhost//controller1

# Rewrite CI certain controllers 
RewriteCond %{THE_REQUEST} directory/(home|other_controller) [NC] 
RewriteRule ^(.*)(home|other_controller)(.*)$ /$1index.php/$2$3 [NC,L] 

第二個問題$1返回空字符串):

這是行不通的http://localhost/dir/home給內部服務器錯誤(過多的重定向)。 但如果我測試添加R=301的代碼,它成功重定向到http://localhost/dir/index.php/home。但這不是我的重定向意圖,我只需要重寫它。

請指教.. :)

回答

1

試試這個:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L] 

你可以把這個目錄中的引導文件(的index.php)是

如果你有。 FastCGI的實現,你需要在重寫規則中添加一個問號:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L] 
+0

謝謝@Repox,但是這個重寫'http:// localhost/directory/controller'到'http:// localhost/index.php/controller',而不是'http:// localhost/directory/index.php/controller' –

+0

簡單...只需添加它...或者在htaccess中設置一個RewriteBase – Brian

+0

@Brian,是否有可能通過正則表達式添加它,而不顯式編寫目錄名稱或重寫基址?所以這個.htaccess文件將是可移植的本地和生產.. –

相關問題