【问题标题】:Cannot remove index.php from Codeigniter for Ubuntu无法从 Codeigniter for Ubuntu 中删除 index.php
【发布时间】:2018-10-16 08:41:17
【问题描述】:

我关注的最后一个在这里How to remove index.php from codeigniter in UBUNTU [duplicate]

我有一个如下所示的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function index()
    {
        $this->load->view('login.html');
    }
}

当我通过这个 URL 访问它时:http://localhost/homerent/Login,我得到了 404 not found。

我从上面的参考链接中的答案遵循

  1. $config['index_page'] = '';
  2. 重启apache2服务:sudo /etc/init.d/apache2 reload
  3. 将以下代码添加到/var/www/html/my_ci_site/.htaccess

    RewriteEngine on
    RewriteBase /
    RewriteCond $1 !^(index\.php|static|robots\.txt|favicon\.ico|uploads|googlexxxxxxxx\.html|mobile.html)
    RewriteRule ^(.*)$ index.php/$1 [L]
    
  4. 将 /etc/apache2/apache2.conf 中 AllowOverride None 的每个实例替换为 AllowOverride All

  5. 启用重写模式:sudo a2enmod rewrite
  6. 最后再次重启apache2服务。

毕竟,我再次访问我的 url http://localhost/homerent/Login 我仍然得到 404 not found。

我不知道这有什么问题。

【问题讨论】:

  • 如果不启用,请确保启用了 mod rewrite 并重新启动 apache。
  • @prasannaputtaswamy,我确实使用了这个命令 sudo a2enmod rewrite,但它仍然无法正常工作。

标签: php apache codeigniter-3 ubuntu-17.10


【解决方案1】:

在 Ubuntu 中你必须做虚拟主机才能工作。 为此首先在/etc/apache2/sites-available 创建yourproject.conf 文件(可能您可能需要root 权限使用sudo 命令)

为此在终端中

 cd /etc/apache2/sites-available

然后

sudo nano yourproject.conf

复制下面的内容并粘贴进去

<VirtualHost *:3434>
      ServerName  localhost

      DirectoryIndex index.php
      DocumentRoot /var/www/html/yourprojectfolder

      <Directory "/var/www/html/yourprojectfolder">
      Options All
      AllowOverride All
      Allow from all
      </Directory>

</VirtualHost>

注意:这里可以使用不同的端口

然后运行

sudo nano /etc/apache2/ports.conf

在此文件中添加一行(不要编辑现有端口)

Listen 3434

然后运行

sudo a2ensite yourproject.conf sudo a2enmod rewrite

config.php

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

yourproject 文件夹内创建.htaccess,内容如下

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

然后重启apache使更改生效

现在您可以通过 url http://localhost:3434(这将加载默认控制器)访问您的站点,无需在 url 中添加项目文件夹

例如 http://localhost/homerent/Login 是现在使用的 url 并且 设置虚拟主机后 你可以使用http://localhost:3434/Login

【讨论】:

    猜你喜欢
    • 2013-08-09
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2023-03-25
    • 2016-04-21
    • 2018-10-17
    相关资源
    最近更新 更多