7

我目前正在使用 CodeIgniter 开发一个网站,最近我偶然发现了一个路由和 .htaccess 问题。

基本上,我的简单网站的结构(为了简单起见,我们将我的项目称为“CIExample”)如下:

-首页 -
关于我们-我们
的服务
-新闻
资讯 -联系我们

我使用页面控制器实现的。该控制器有 5 个函数调用各自的页面视图,即:

Home(),它调用视图'Home'
About(),它调用视图'About Us'等等。

以前,要访问“主页”,我需要http://localhost/CIExample/index.php/page/home在 url 窗口中输入内容,但在设置以下路由后,我能够删除“页面”(类名)部分:

$route['default_controller'] = 'page/home';
$route['home'] = 'page/home';
$route['about'] = 'page/about';
$route['service'] = 'page/service';
$route['news'] = 'page/news';
$route['contact'] = 'page/contact';

但是,当我尝试删除“index.php”时,棘手的部分就来了。我希望能够通过键入来访问主页http://localhost/CIExample/home

所以我在CI论坛/教程/堆栈溢出上做了很多搜索,发现了一些.htaccess文件的代码:

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

http://ellislab.com/forums/viewthread/197675/#929904
我尝试了两种代码,但都没有工作..

http://localhost/CIExample/home会引导我到 404 not found 页面,但http://localhost/CIExample/index.php/home可以正常工作。

我想知道出了什么问题?是我的 routes.php 或 .htaccess 还是两者兼而有之?谢谢。

注意:我还更改了我的 'config/config.php' 文件 ->$config['index_page'] = '';

编辑: 终于成功了!调整 config 文件夹中的 config.php 文件并设置以下内容$config['uri_protocol'] = 'PATH_INFO';(来自“AUTO”)。

可用值:

| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO

不知道有什么不同,但根据http://www.jotorres.com/2011/12/removing-index-php-from-url-in-codeigniter/中的评论之一,相同的代码可能/可能不起作用生产服务器。

任何人都可以解释原因吗?

4

6 回答 6

15

您只需将其粘贴到您的 .htaccess 文件中:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

并对您的 application/config/config.php 文件进行此更改:

$config['index_page'] = '';
于 2013-07-30T08:09:00.020 回答
4

我使用了以下方法,它在UBUNTU中运行良好......!!

首先验证您是否phpinfo()在 index.php 文件中启用了 mod_rewrite(您可以使用 验证)。

IF DISABLED mod_rewrite then RUN: 1. sudo a2enmod rewrite 2.只需将Tag(<'Directory /var/www/'>) 中的sudo gedit /etc/apache2/sites-available/defaut “AllowOverride None”替换为“ ” 3. 然后请运行AllowOverride Allsudo service apache2 restart

Ok Good... 现在在 Codeigniter 的根目录 -> 查找.htaccess文件,如果找不到,请按ctrl+h,仍然找不到然后创建一个文件.htaccess

.htaccess 粘贴以下代码:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /ROOT DIRECTORY NAME/

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

好的,最后只是更换ROOT DIRECTORY NAME

不要忘记 从文件中删除 使该行像 index.phpapplication/config/config.php$config['index_page'] = '';

于 2015-04-09T13:29:08.543 回答
2

我将这个 htaccess 用于我所有的 CodeIgniter 项目。它也支持子文件夹。

<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>
于 2013-03-18T16:39:22.493 回答
1

在你的system/application/config/config.php, 改变

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

$config['index_page'] = "";

在您的 .htaccess 中,添加以下内容:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
于 2019-01-10T06:21:38.333 回答
0
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

居住:

#RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

当地的:

#RewriteRule .* index.php/$0 [PT,L]

于 2018-05-21T12:50:20.943 回答
0
for Codeigniter 3.1.4

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php56” package as the default “PHP” programming language.
<IfModule mime_module>
  AddType application/x-httpd-ea-php56 .php .php5 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php5_module>
   php_flag asp_tags On
   php_flag display_errors On
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 512M
   php_value post_max_size 128M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
   php_value upload_max_filesize 128M
   php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
于 2017-11-22T19:18:27.130 回答