2

我关注的最后一个是这里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 Noneto的每个实例AllowOverride All

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

毕竟,我再次访问我的网址,http://localhost/homerent/Login但仍然找不到 404。

我不知道这有什么问题。

4

1 回答 1

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

于 2018-08-21T15:09:57.937 回答