2010-06-02 120 views
1

我是CodeIgniter和路由的新手。codeigniter中的路由問題

我有一個Login控制器,其index()加載一個視圖來輸入用戶名/密碼。在該視圖中,表單有action="login/authenticate"。 Login-> authenticate()確定登錄是否有效。如果它是有效的,redirect('lobby'),如果不是redirect('login')

routes.php文件:

$route['default_controller'] = "login" 

的config.php:

$config['base_url'] = "http://localhost/dts/"; 
$config['index_page'] = "index.php"; 

的問題是,當我去http://localhost/dts/,點擊登錄,我正確(?)重定向到http://localhost/dts/login/authenticate,但瀏覽器顯示爲Object not found!。但是,當我去http://localhost/dts/index.php/(帶斜線),它工作正常(我重定向到http://localhost/dts/index.php/login/authenticate,和我登錄)

我試圖用的.htaccess刪除「的index.php」:

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

它將不再打開連http://localhost/dts/

我很困惑..發生了什麼事?

+0

即使我很困惑。 :) – User 2010-06-02 15:08:40

回答

4

的問題是與你的.htaccess文件的最後一行。

由於您的應用程序位於子目錄(「dts」)中,因此「index.php」前面的正斜槓將不起作用。試着讓你的文件看起來像這樣將其取出:

RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ index.php/$1 [L] 
+0

做了詭計的人!謝謝! – Obay 2010-06-03 14:52:33

+0

它也幫助了我。太多了。 – Jason 2012-12-14 12:26:50

3

按照this article on the CodeIgniter wiki正確設置你的.htaccess如下:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase/

    #Removes access to the system folder by users. 
    #Additionally this will allow you to create a System.php controller, 
    #previously this would not have been possible. 
    #'system' can be replaced if you have renamed your system folder. 
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #When your application folder isn't in the system folder 
    #This snippet prevents user access to the application folder 
    #Submitted by: Fabdrol 
    #Rename 'application' to your applications folder name. 
    RewriteCond %{REQUEST_URI} ^application.* 
    RewriteRule ^(.*)$ /index.php?/$1 [L] 

    #Checks to see if the user is attempting to access a valid file, 
    #such as an image or css document, if this isn't true it sends the 
    #request to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?/$1 [L] 
</IfModule> 

<IfModule !mod_rewrite.c> 
    # If we don't have mod_rewrite installed, all 404's 
    # can be sent to index.php, and everything works as normal. 
    # Submitted by: ElliotHaughin 

    ErrorDocument 404 /index.php 
</IfModule> 
+1

+1鏈接到文檔。 – User 2010-06-02 15:08:20