代码之家  ›  专栏  ›  技术社区  ›  Houy Narun

无法删除索引。来自Ubuntu Codeigniter的php

  •  2
  • Houy Narun  · 技术社区  · 7 年前

    我跟踪的最后一个在这里 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。

    我在参考链接中遵循上述答案

    1. $配置['index\u page']=“”;
    2. 重新启动apache2服务:sudo/etc/init。d/apache2重新加载
    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. 替换的每个实例 AllowOverride None AllowOverride All 在/etc/apache2/apache2中。形态

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

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

    我不知道那是怎么回事。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Visakh V Nair    6 年前

    在Ubuntu中,你必须使用虚拟主机才能工作。 对于第一个 /etc/apache2/sites-available 创造 yourproject.conf 文件(可能是您可能需要根权限使用 sudo 命令)

    对于该in终端

     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'] = '';
    

    创造htaccess内部 yourproject 包含以下内容的文件夹

    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 (这将加载defaulf控制器),无需在url中添加项目文件夹

    例如 http://localhost/homerent/Login url是否正在使用,以及 设置虚拟主机后 您可以使用 http://localhost:3434/Login