【问题标题】:Remove index.php from url in codeigniter using htaccess使用 htaccess 从 codeigniter 中的 url 中删除 index.php
【发布时间】:2018-09-17 04:00:33
【问题描述】:

我试图在我尝试使用 .htaccess 文件创建的 codeigniter 中从我的 url 中删除 index.php,但它不起作用。

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

我尝试使用此代码,这些是 mu bse 和站点 url。我对如何正确使用 htaccess 知之甚少。任何帮助都是可观的。

$config['base_url'] = 'http://localhost/xxxx/';

$config['site_url'] = 'http://localhost/xxxx/index.php';

【问题讨论】:

  • 只需将RewriteRule ^(application|system|\.svn) index.php/$1 [L] 替换为RewriteRule ^(.*)$ index.php?/$1 [L]

标签: php .htaccess codeigniter


【解决方案1】:

打开 config.php 并进行以下替换

发件人:

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

收件人:

$config['index_page'] = "";

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

在.htaccess文件中添加

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

【讨论】:

  • 我也尝试过使用它,但是 url 中的错误是一个额外的 '/' 出现在 index.php 的位置。
【解决方案2】:

希望对你有帮助:

Options +FollowSymlinks -Indexes
RewriteEngine on
DirectoryIndex index.php
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'] = '';
    $config['base_url'] = 'http://localhost/foldername/';

更多:https://www.codeigniter.com/user_guide/general/urls.html

【讨论】:

  • 我有一个疑问,我应该把这个 htaccess 文件放在哪里,我应该把它放在系统文件夹中还是放在应用程序文件夹之外。
  • 就在您的应用程序文件夹之外意味着在您的应用程序文件夹内
  • 我应该从我的 site_url 中删除 index.php。
  • 不用担心直接使用 base_url() 或 site_url()
  • ok by using this index.php 被删除,但 url 显示不正确,即在 index,php 的位置出现了一个额外的“/”
【解决方案3】:

尝试下面的代码从 url 中删除 index.php,在根文件夹中创建一个 htaccess 文件并将下面的代码粘贴到其中。

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

要删除 url 中的 index.php,您需要更改配置。 在 config.php 文件中更改以下内容

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

$config['index_page'] = "";

这将从 url 中删除 index.php。

【讨论】:

    【解决方案4】:

    如果它对这里的任何人有帮助,这个问题让我发疯了,我的解决方案是从项目文件夹中删除大写字母(它最初是 localhost/working/CourseIgnite/)。

    所以在多次尝试解决该问题后,我将站点更改为 localhost/working/courseignite/

    然后使用每个人发布的解决方案都有效。 Mod重写,更改配置中的base url和index,并在htaccess中使用以下内容。

    希望这会有所帮助,因为它让我发疯了。

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

    然后在应用程序/配置中

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

    $config['index_page'] = "";
    

    在应用程序/配置中

    $config['base_url'] = "yourlocalhostip/working/courseignite/";  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 2021-10-02
      • 2015-01-20
      • 2023-03-07
      • 1970-01-01
      • 2011-05-20
      • 2017-03-15
      相关资源
      最近更新 更多