【问题标题】:Activating .htaccess in my CodeIgniter project (Removing "index.php")在我的 CodeIgniter 项目中激活 .htaccess(删除“index.php”)
【发布时间】:2014-01-20 11:55:42
【问题描述】:

我试图从我的项目中删除 index.php 段,但我不能。我测试了 StackOverflow 中引用的几篇文章,但仍然无法。

这是我的项目结构:

(In /var/www/project directory)
project
--assets
--system
--.htaccess
--index.php
--info.php
--quickmenu.php

PHP version: 5.3.10
SO: Ubuntu 12.04
Apache version: 2.2.22
CodeIgniter: 2.1.3

我的 config.php 文件:

$config['base_url'] = "HTTP://".$_SERVER['HTTP_HOST']."/project/";

$config['index_page'] = "";

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


$config['url_suffix'] = "";


$config['language'] = "portugues";

$config['charset'] = "iso-8859-1";


$config['enable_hooks'] = FALSE;


$config['subclass_prefix'] = 'MY_';



$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';


$config['enable_query_strings'] = FALSE;
$config['directory_trigger'] = 'd';  // experimental not currently in use
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';

$config['log_threshold'] = '1';


$config['log_path'] = '';

$config['log_date_format'] = 'Y-m-d H:i:s';


$config['cache_path'] = '/var/ci/cache/';


$config['encryption_key'] = "q7uP3mprMYuFFvbNcXyC6uPXs2ZUezxh";


$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_encrypt_cookie']          = FALSE;
$config['sess_use_database']            = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent']         = TRUE;
$config['sess_time_to_update']      = 300;
$config['sess_storage']                 = 'database';

$config['sess_database']                = 'default';
$config['sess_timeout']                 = 7200;
$config['sess_destroy_on_timeout']      = FALSE;
$config['sess_update_interval']         = 180;
$config['sess_gc_probability']          = 10;
$config['sess_http_only']               = FALSE;
$config['sess_secure']                  = FALSE;
$config['sess_forwarded_ip']            = FALSE;


$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";

$config['global_xss_filtering'] = FALSE;

$config['compress_output'] = FALSE;


$config['time_reference'] = 'local';


$config['rewrite_short_tags'] = FALSE;

/* End of file config.php */
/* Location: ./system/application/config/config.php */

我的 .htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /project/

    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>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule> 

补充:

使用网址不起作用:

http://localhost/project/#../directory/controller/method

使用这些网址可以:

http://localhost/project/#index.php/directory/controller/method
http://localhost/project/index.php/#directory/controller/method

【问题讨论】:

标签: php apache .htaccess codeigniter


【解决方案1】:

尝试将 uri_protocol 从 AUTO 更改为 REQUEST_URI

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

你可以参考这个链接:http://taggedzi.com/articles/display/codeigniter-2-htaccess-and-friendly-urls

【讨论】:

  • 是的,我试过这个,但不起作用。我尝试了“uri_protocol”的所有选项,但仍然不起作用。
【解决方案2】:

如果你是在服务器上上传,那么 Remove RewriteBase /project/

并写 RewriteBase /

我也犯了同样的错误。我刚刚测试过,效果很好。

【讨论】:

  • 试着把 $config['uri_protocol'] = 'AUTO';
【解决方案3】:

将您的config.php 更改为:

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

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

然后将此规则放入您的/project/.htaccess

RewriteEngine On

RewriteBase /project/

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

还要确保.htaccess 中不存在/project/directory/

【讨论】:

  • 你在努力工作吗:http://localhost/project/#../directory/controller/method?
  • # 之后的任何内容都不会被 Web 服务器接收到。
  • 如果我在 # 之后插入“index.php”。此示例有效:localhost/project/#index.php/directory/controller/method
  • # 之后可能有一些 Javascript sn-p 将字符串传递给 Web 服务器。
  • 是的,我有一个 Ajax Jquery 用于加载控制器。
【解决方案4】:

我解决了这个问题。

缺少在我的 PHP 上安装 mod_rewrite 包。所以在这之后只需配置 config/config.php 和 .htaccess 文件。

在我用于在我的 UBUNT 中安装 PHP 的 mod_rewrite 模块的文章下方:

How to enable mod_rewrite for Apache 2.2

感谢大家的建议,如果不是他们,我不会尝试这种解决方法。

【讨论】:

    【解决方案5】:

    我找到了适合我的确切解决方案。你可以试试。 我认为最重要的是 RewriteBase / 和 ./CI/ 将是您的应用程序包含的文件夹名称。示例:CI/应用程序 所以要小心。

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

    但如果你是 svn 服务器,那么使用: 重写规则 ^(.*)$ ./index.php/$1 [L]

    如果您使用/不使用 index.php,这些将起作用 这里`例如:http://kabircse.tk/CI/http://kabircse.tk/CI/index.php

    两者都会找到你的路由控制器。如果你的路由控制器是 mc 那么它会发现它是http://kabircse.tk/CI/mc

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2015-03-09
      • 1970-01-01
      • 2016-05-13
      • 2011-10-09
      • 2012-08-15
      • 1970-01-01
      • 2023-03-16
      • 2014-08-24
      相关资源
      最近更新 更多