codeigniter中的路由问题

时间:2010-06-02 14:54:53

标签: php codeigniter routing

我是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/

我很困惑......发生了什么事?

3 个答案:

答案 0 :(得分:4)

问题在于.htaccess文件的最后一行。

由于您的应用程序位于子目录(“dts”)中,因此“index.php”前面的正斜杠将不起作用。尝试删除它,以便您的文件如下所示:

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

答案 1 :(得分: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>

答案 2 :(得分:1)

您是否在配置中更改了此内容:

$config['index_page'] = "index.php";

对此:

$config['index_page'] = "";

另外,请阅读Jamie Rumbelow的这篇文章:

http://jamieonsoftware.com/blog/entry/the-best-codeigniter-.htaccess

这就是我一直使用的.htaccess,它对我很有用。