Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 404 on Apache server with codeigniter

It's my first time i'm uploading codeigniter project to production and i'm getting error 404 from Apache server.

error : Not Found The requested URL /index.php was not found on this server. Apache/2.4.18 (Ubuntu) Server at roy.my-domain.com Port 80

I've read every article for 2 days and didn't found a solution....

So here are my settings :

Checked for rewrite mod in Apache - got "Module rewrite already enabled" My project is in /var/www/roy/ so the url is roy.my-domain.com/roy

  1. Apache2.conf

    <Directory /var/www/html/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    </Directory>
    AccessFileName .htaccess
    
  2. .htaccess

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /roy/
    RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|asset|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>
    
  3. config.php

    $config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/roy/';
    $config['index_page'] = '';
    
  4. autoload.php (i'm using Twig)

    $autoload['libraries'] = array('database','session','twig');
    
    • I added permissions to both logs and cache dir
  5. The controllers structure

    name Login.php - 
    class Login extends CI_Controller
    
like image 541
RoyBarOn Avatar asked Apr 10 '26 01:04

RoyBarOn


1 Answers

Create a site folder inside your html one. Then, put your roy site there. Then, I would do it this way:

1 - apache2/sites-enabled/roy.my-domain.com.conf

<VirtualHost roy.my-domain.com:80>
    DocumentRoot /var/www/html/site
    ServerName roy.my-domain.com
    <Directory /var/www/html/site>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

2 - .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

3 - If it's not development, it's never a good practice to use the $_SERVER['HTTP_HOST'] superglobal. Can be changed from the client side. Just use the right url.

After you're done all that, restart apache. In Ubuntu is: $ sudo service apache2 restart

like image 168
Matías Navarro Carter Avatar answered Apr 12 '26 16:04

Matías Navarro Carter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!