简体   繁体   中英

Codeigniter Htaccess and URL Redirect issues

I am trying to use .htaccess on CodeIgniter, but it is not working.

I have already set:

AllowOverride All
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

I am using XAMPP on Windows.

My .htaccess :

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

My URL still contains index.php and, if I try to remove it manually, it returns a 404 error

Also my other concern is my login page: whenever I login, my URL is stuck on the check page.

I was wondering how do I rewrite my URL to change to home page instead of staying on the check page.

public function check(){
        $this->load->model('check');
        $logcheck = $this->check->check($this->input->post('username'),$this->input->post('password'));

        if($logcheck == "admin"){
            $this->load->view('home');
        }else{
            $this->load->view('login');
        }
    }

This simple .htaccess will remove your index.php

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

Change your check function like this

 public function check(){
    $this->load->model('check');
    $logcheck = $this->check->check($this->input->post('username'),$this->input->post('password'));

    if($logcheck == "admin"){
        //may  be you need to set login credential into session
        redirect('Phonebook/home/');
        //$this->load->view('home');
    }else{
        $this->load->view('login');
    }
}

and your home function will be like this

public function home(){
      //remember you need to check login validation from session
      $this->load->view('home');
}

to use redirect function remember you have url helper loaded.
May be this help you

I have found that if you go to this place here http://www.insiderclub.org/downloads and then click on the link "HERE IT IS" download the htaccess zip folder there are about 5 different suited ones for codeigniter for htaccess

Not sure where this AllowOverride All comes into play?

Also

$config['uri_protocol'] = 'REQUEST_URI';

Make

$config['uri_protocol'] = 'AUTO';

$config['base_url'] = 'http://localhost/yourproject/';

$config['index_page'] = '';

Make sure you have configured your route.php

Main htaccess root. You do not need to touch the other .htaccess

The first one you will find in zip is this one

Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Seems to work fine with my xampp and codeigniter 2 & 3 but try the ones on download.

This is the second one in zip folder for htaccess think more for wamp etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM