从CodeIgniter URL中删除index.php

时间:2012-02-20 21:17:58

标签: php apache codeigniter

我正在尝试访问没有'index.php'的CodeIgniter网址。这是我采取的步骤:

  1. 已启用已检查的mod_rewrite - 我设置了一条规则,将所有请求重定向到google,这有效。还检查了“AllowOverride All”已设置

  2. 添加.htaccess文件,内容如下:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    
  3. 在网络服务器上设置我的目录结构,如下所示:

    /home/wwwsunde/
    -> application
    -> system
    -> public_html
        -> index.php
        -> .htaccess
    
  4. 更新了我的应用程序/配置文件,以便系统和应用程序路径为“../system”和“../ application”

  5. 尝试从2个网址访问该网站

    http://109.234.194.207/~wwwsunde/index.php/welcome/membership WORKS
    http://109.234.194.207/~wwwsunde/welcome/membership DOES NOT WORK
    
  6. 我还根据指南将CodeIgniter的索引页变量设置为空白

  7. 显示错误消息:

    The requested URL /home/wwwsunde/public_html/index.php/welcome/membership was not found on this server.
    

    我不知道可能出现什么问题 - 这是Apache或服务器问题,但我不确定是什么......

2 个答案:

答案 0 :(得分:3)

哦......我知道为什么......把它作为你的重写规则:

RewriteRule .* index.php/$0 [PT]

你拥有的[L]只是一个'最后一条规则',但是你不想做退出重写你想要悄悄地处理背景shell游戏。这将是一个无限循环。如果这不起作用,请在重写中指定完整的URL,如:

 RewriteRule .* http://109.234.194.207/~wwwsunde/index.php/$0  [PT]

答案 1 :(得分:1)

你从哪里获得索引页应该是空白的?你的意思是index.php?它应该是这样的:

<?php
/*
|---------------------------------------------------------------
| PHP ERROR REPORTING LEVEL
|---------------------------------------------------------------
|
| By default CI runs with error reporting set to ALL.  For security
| reasons you are encouraged to change this when your site goes live.
| For more info visit:  http://www.php.net/error_reporting
|
*/
        error_reporting(0);
//      ini_set("display_errors", "on");

/*
|---------------------------------------------------------------
| SYSTEM FOLDER NAME
|---------------------------------------------------------------
|
| This variable must contain the name of your "system" folder.
| Include the path if the folder is not in the same  directory
| as this file.
|
| NO TRAILING SLASH!
|
*/
        $system_folder = "system";

/*
|---------------------------------------------------------------
| APPLICATION FOLDER NAME
|---------------------------------------------------------------
|
| If you want this front controller to use a different "application"
| folder then the default one you can set its name here. The folder
| can also be renamed or relocated anywhere on your server.
| For more info please see the user guide:
| http://codeigniter.com/user_guide/general/managing_apps.html
|
|
| NO TRAILING SLASH!
|
*/
        $application_folder = "application";

/*
|===============================================================
| END OF USER CONFIGURABLE SETTINGS
|===============================================================
*/


/*
|---------------------------------------------------------------
| SET THE SERVER PATH
|---------------------------------------------------------------
|
| Let's attempt to determine the full-server path to the "system"
| folder in order to reduce the possibility of path problems.
| Note: We only attempt this if the user hasn't specified a
| full server path.
|
*/
if (strpos($system_folder, '/') === FALSE)
{
        if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE)
        {
                $system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
        }
}
else
{
        // Swap directory separators to Unix style for consistency
        $system_folder = str_replace("\\", "/", $system_folder);
}

/*
|---------------------------------------------------------------
| DEFINE APPLICATION CONSTANTS
|---------------------------------------------------------------
|
| EXT           - The file extension.  Typically ".php"
| FCPATH        - The full server path to THIS file
| SELF          - The name of THIS file (typically "index.php")
| BASEPATH      - The full server path to the "system" folder
| APPPATH       - The full server path to the "application" folder
| MEDIAPATH - The full server path to the "media" folder
*/
define('EXT', '.'.pathinfo(__FILE__, PATHINFO_EXTENSION));
define('FCPATH', __FILE__);
define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
define('BASEPATH', $system_folder.'/');
define('MEDIAPATH', dirname(__FILE__).'/media');

if (is_dir($application_folder))
{
        define('APPPATH', $application_folder.'/');
}
else
{
        if ($application_folder == '')
        {
                $application_folder = 'application';
        }

        define('APPPATH', BASEPATH.$application_folder.'/');
}

/*
|---------------------------------------------------------------
| LOAD THE FRONT CONTROLLER
|---------------------------------------------------------------
|
| And away we go...
|
*/
require_once BASEPATH.'codeigniter/CodeIgniter'.EXT;

/* End of file index.php */
/* Location: ./index.php */

请注意,我从我们的一个CI项目中随机抓取了这个,所以它可能是自定义的 - 我真的不记得 - 但它绝对不是空白。

另外,配置:

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

和htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>