【问题标题】:CodeIgniter Remove index.php with .htaccessCodeIgniter 使用 .htaccess 移除 index.php
【发布时间】:2012-12-04 23:51:06
【问题描述】:

我已经为此工作了一个小时,所以我想我不妨问问。

我正在尝试从我的 CodeIgniter 应用程序的 URL 中删除 index.php,但无法做到。

该应用程序在我办公室的专用服务器上运行,我通过 URL http://smr_local 访问该应用程序

这是我的基本虚拟主机块

<VirtualHost 192.168.1.90:80> 
    ServerAdmin admin@server.com
    DocumentRoot "/var/www/smr.dev/app"
    ServerName smr_local
    ErrorLog "/etc/httpd/logs/error_log"
    CustomLog "/etc/httpd/logs/access_log" common
    <Directory /var/www/smr.dev/app>
        Order allow,deny
        Allow from all
        AllowOverride All
    </Directory>
    <IfModule mod_rewrite.c>
        RewriteEngine on
    </IfModule>
</VirtualHost>

这是我的 .htaccess 文件

RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

还有我的配置文件

$config['base_url']    = "http://smr_local/";
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

现在,当我尝试访问我的基本 URL http://smr_local/user/courses 时,我的 apache 错误日志中出现错误,我得到了这个。

File does not exist: /var/www/smr.dev/app/user

我真的不知道接下来该尝试什么。任何帮助将不胜感激。

【问题讨论】:

    标签: apache codeigniter


    【解决方案1】:

    你查过官方user guide吗?

    example.com/index.php/news/article/my_article
    

    您可以使用带有一些简单规则的 .htaccess 文件轻松删除此文件。以下是此类文件的示例,使用“否定”方法,其中除了指定项目之外的所有内容都被重定向:

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

    在上面的示例中,除 index.php、images 和 robots.txt 之外的任何 HTTP 请求都被视为对 index.php 文件的请求。


    我之前一直在使用 Codeigniter,这就是我让它工作的方式:

    这是我的.htaccess(假设您有默认文件夹结构):

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # Removes access to the system folder by users.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
    
        # Prevents user access to the application folder
        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>
        ErrorDocument 404 /index.php
    </IfModule> 
    

    这是config.php的相关部分:

    $config['base_url'] = '';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    $config['url_suffix'] = '';
    

    如果这没有帮助,您的问题可能在 Apache 配置中。

    然后提供您的httpd.conf 和任何其他相关配置,而不仅仅是virtual-hosts

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 1970-01-01
      • 2016-05-13
      • 2012-08-15
      • 2015-03-09
      • 1970-01-01
      • 2013-03-07
      • 2018-12-04
      • 1970-01-01
      相关资源
      最近更新 更多