authenticate" />
开发者

routing problem in codeigniter

开发者 https://www.devze.com 2023-01-02 04:56 出处:网络
I\'m new to CodeIgniter and routing. I have a Login controller whose index() loads up a view to enter a username/password. In the view, the form has action=\"login/authenticate\". Login->authenticate

I'm new to CodeIgniter and routing.

I have a Login controller whose index() loads up a view to enter a username/password. In the view, the form has action="login/authenticate". Login->authenticate() determines if the login is valid or not. If it's valid, redirect('lobby'), if not redirect('login')

routes.php:

$route['default_controller'] = "login"

config.php:

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

The problem is that when i go to http://localhost/dts/ , click login, I am correctly (?) redirected to http://localhost/dts/login/authenticate but the browser says Object not found!. But when I go to http://localhost/dts/index.php/ (with trailing slash), it works correctly (I get redirected to http://localhost/dt开发者_如何学Cs/index.php/login/authenticate, and am logged in)

I tried removing "index.php" by using a .htaccess:

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

and it would no longer open even the http://localhost/dts/

I'm confused.. what's going on?


The issue is with the last line of your .htaccess file.

Since your application is in a subdirectory ("dts"), the forward slash in front of the "index.php" won't work. Try removing it so that your file looks like this:

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


Follow this article on the CodeIgniter wiki to properly setup your .htaccess as follows:

<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>


Did you change this in your config:

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

To this:

$config['index_page'] = "";

Also, read this post by Jamie Rumbelow:

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

Thats the .htaccess I've always used and it works great for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号