且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CodeIgniter htaccess和URL重写问题

更新时间:2023-02-23 13:07:44

有3个步骤来删除 index.php


  1. application / config.php 文件中进行以下更改

      $ config ['base_url'] ='http://'.$_SERVER ['SERVER_NAME']。'/您的Ci folder_name'; 
    $ config ['index_page'] ='';
    $ config ['uri_protocol'] ='AUTO';


  2. .htaccess 您的根目录使用以下代码

      RewriteEngine on 
    RewriteCond $ 1!^(index\.php | resources | \.txt)
    RewriteCond%{REQUEST_FILENAME}!-f
    RewriteCond%{REQUEST_FILENAME}!-d
    RewriteRule ^(。*)$ index.php / $ 1 [L,QSA]


  3. 启用重写引擎(如果尚未启用)



    i。首先,使用以下命令启动它:

      a2enmod重写

    ii。编辑档案 / etc / apache2 / sites-enabled / 000-default



    全部更改 AllowOverride None AllowOverride All



    注意:在最新版本中,您需要更改 apache2 / apache2.conf 文件



    iii。使用以下命令重新启动服务器:

      sudo /etc/init.d/apache2 restart 



I have never used CodeIgniter before, let alone ANY php framework and I thought I would give it a try. Everything is going fine except I cannot seem to remove the index.php from the URL and still access my pages.

I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.

I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about

The controller for the page is: /application/controllers/about.php
The Model for the page is: /application/models/about_model.php
And the View for the page is: /application/views/about_page.php

I have searched for a solution to this issue, but haven't been able to find one. Here is where I have already searched:

CodeIgniter - removing index.php
Codeigniter - how to remove the index.php from url?
http://www.farinspace.com/codeigniter-htaccess-file/

CodeIgniter comes with a .htaccess file in the 'application' folder which contains only Allow Deny From All. So I created a new .htaccess file in the root directory, http://localhost/ci/.htaccess and added this code to it:

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

When the .htaccess file is in the root directory I get a 500 Internal Server Error. When I copy the exact same file into the applications folder the 500 error goes away, but I still cannot access the about page by using localhost/ci/about

I have already changed $config['index_page'] = 'index.php'; to $config['index_page'] = ''; AND I tried changing $config['uri_protocol'] = 'AUTO'; to $config['uri_protocol'] = 'REQUEST_URI'; but I am still getting the Internal Server Error.

I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.

Does anyone have any ideas why this isn't working or how I can get this work? I know there are alot of questions on *** on this subject but I couldn't find one that answered my question.

Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/about or do I have to create an 'about' directory in the 'application' directory?

There are 3 steps to remove index.php.

  1. Make below changes in application/config.php file

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    

  2. Make .htaccess file in your root directory using below code

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

  3. Enable the rewrite engine (if not already enabled)

    i. First, initiate it with the following command:

    a2enmod rewrite
    

    ii. Edit the file /etc/apache2/sites-enabled/000-default

    Change all AllowOverride None to AllowOverride All.

    Note: In latest version you need to change in /etc/apache2/apache2.conf file

    iii. Restart your server with the following command:

    sudo /etc/init.d/apache2 restart