Codeigniter Htaccess和URL重定向问题

时间:2015-02-14 15:29:49

标签: php apache .htaccess codeigniter mod-rewrite

我正在尝试在CodeIgniter上使用.htaccess,但它无效。

我已经设定了:

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

我在Windows上使用XAMPP。

我的.htaccess

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

我的网址仍然包含index.php,如果我尝试手动删除,则会返回404错误

另外我的另一个问题是我的登录页面:每当我登录时,我的网址都会停留在检查页面上。

我想知道如何重写我的网址以更改为主页而不是留在检查页面。

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');
        }
    }

2 个答案:

答案 0 :(得分:3)

这个简单的.htaccess将删除你的index.php

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

像这样更改您的支票功能

 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');
    }
}

你的家庭功能将是这样的

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

要使用redirect功能,请记住您已加载url个助手 可能对你有帮助

答案 1 :(得分:0)

我发现如果你去这个地方http://www.insiderclub.org/downloads,然后点击链接"这就是"下载htaccess zip文件夹,大约有5种不同的适用于htaccess的codeigniter

不确定 AllowOverride All 在哪里发挥作用?

另外

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

制作

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

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

$config['index_page'] = '';

确保您已配置route.php

主要的htaccess root。你不需要触碰另一个.htaccess

你会在zip中找到的第一个就是这个

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]

似乎与我的xampp和codeigniter 2& 3但请尝试下载。

这是htaccess的zip文件夹中的第二个,为wamp等考虑更多。

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