【问题标题】:How to remove index.php from URL in CodeIgniter using .htaccess如何使用 .htaccess 从 CodeIgniter 中的 URL 中删除 index.php
【发布时间】:2017-05-20 19:46:03
【问题描述】:

我知道这个问题以前被问过很多次,我已经解决了大部分问题,但找不到解决方案。我已经尝试了herehere 和 CodeIgniter 官方文档中给出的大部分答案。并且每次都重新启动apache。它对我不起作用。 mod_rewrite 已在我的 apache 服务器中启用。

奇怪的是,我有一个 WAMP 的工作示例(在我的笔记本电脑中):

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

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

它在 WAMP 中完美运行,但在 LAMP 中不起作用。任何形式的帮助表示赞赏。

【问题讨论】:

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

请使用以下代码:

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)

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

我用过这个,它正在工作。

【讨论】:

    【解决方案2】:

    在您的 .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>
    
    <Directory "/path/to/document/root/">
      AllowOverride All
      Allow from All
    </Directory>
    

    【讨论】:

      【解决方案3】:

      就这样……

      .htaccess 在你的根目录中使用以下代码..

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

      application/config.php中设置配置如下..

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

      【讨论】:

        【解决方案4】:

        我在以下 .htaccess 代码的帮助下使它工作:

        RewriteEngine on
        RewriteCond $1 !^(index\.php|public|\.txt)
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?$1
        

        我更新了/etc/apache2/apache2.conf 文件的&lt;Directory /var/www/&gt; 部分:

        <Directory /var/www/>
                Options Indexes FollowSymLinks
                AllowOverride all
                Require all granted
        </Directory>
        

        它解决了所有问题,现在可以正常工作了。

        【讨论】:

          【解决方案5】:

          这是我的方法:

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

          并且还在 config.php 中替换

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

          $config['index_page'] = ""
          

          并且也在同一个文件中替换

          $config['uri_protocol'] ="AUTO"
          

          通过

          $config['uri_protocol'] = "REQUEST_URI"
          

          【讨论】:

            猜你喜欢
            • 2018-09-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-02
            • 2015-01-20
            • 2023-03-07
            • 1970-01-01
            • 2012-08-10
            相关资源
            最近更新 更多