Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pemby's avatar

Configuration issue? Ubuntu/Apache2 14.04 Simple route will not return correctly.

Laravel will render the default / root page normally. but will not render default routes or error pages.

Here is some info

Server Directory Tree

/var/www/
└── html
    ├── access.log
    ├── app
    ├── artisan
    ├── bootstrap
    ├── composer.json
    ├── composer.lock
    ├── config
    ├── database
    ├── error.log
    ├── gulpfile.js
    ├── package.json
    ├── phpspec.yml
    ├── phpunit.xml
    ├── public
    ├── readme.md
    ├── resources
    ├── server.php
    ├── storage
    ├── tests
    └── vendor

Views (test view is copy of the default welcome view)

/var/www/html/resources/views/
├── errors
│   └── 503.blade.php
├── test.blade.php
├── vendor
└── welcome.blade.php    

Routes

<?php    

Route::get('/', function () {
    return view('welcome');
});    


Route::get('test', function () {
    return view('test');
});    


Route::get('anotherTest', function () {
    return 'Hello World';
});   

Apache config

<VirtualHost *:80>    

    # The location of our projects public directory.
    DocumentRoot    /var/www/html/public    

    # Useful logs for debug.
    CustomLog       /var/www/html/access.log common
    ErrorLog        /var/www/html/error.log    

    # Rewrites for pretty URLs, better not to rely on .htaccess.
    <Directory /www/var/html>
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>
    </Directory>    

</VirtualHost>

using default (provided) .htaacess file

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>    

    RewriteEngine On    

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]    

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Also my error log is empty But access log shows 404 and 496

admini@linux:/var/www/html$ cat error.log
admini@linux:/var/www/html$ cat access.log
192.168.1.130 - - [24/Jul/2015:15:51:53 -0700] "GET / HTTP/1.1" 200 1448
192.168.1.130 - - [24/Jul/2015:15:54:46 -0700] "GET /test HTTP/1.1" 404 496
192.168.1.130 - - [24/Jul/2015:15:56:37 -0700] "GET /test HTTP/1.1" 404 496
192.168.1.130 - - [24/Jul/2015:15:57:11 -0700] "GET /test HTTP/1.1" 404 496
192.168.1.130 - - [24/Jul/2015:15:57:13 -0700] "GET /test HTTP/1.1" 404 495
192.168.1.130 - - [24/Jul/2015:16:14:53 -0700] "GET /test HTTP/1.1" 404 496
192.168.1.130 - - [24/Jul/2015:16:14:53 -0700] "GET /test HTTP/1.1" 404 495
192.168.1.130 - - [24/Jul/2015:16:18:16 -0700] "GET /test HTTP/1.1" 404 496
192.168.1.130 - - [24/Jul/2015:16:18:17 -0700] "GET /test HTTP/1.1" 404 495
admini@linux:/var/www/html$
0 likes
8 replies
henrique's avatar

Try running this two commands:

sudo a2enmod rewrite
sudo service apache2 restart
pemby's avatar

No sorry any other ideas?

admini@linux:/var/www/html/public$ sudo a2enmod rewrite
[sudo] password for admini:
Module rewrite already enabled
tankerkiller125's avatar

It appears that your Apache config does not contain all of the .htaccess (For example)

# Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]    

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d

Is all missing from the Apache config (just below enabling the Rewrite engine and above the RewriteCond %{REQUEST_FILENAME} !-f

Those lines are actually some of the most important lines of the .htaccess file. And it appears that you may also have .htaccess files disabled server side (which would be suggested performance wise)

pemby's avatar

@tankerkiller125 Thanks for you help. I am not following how you are suggesting I edit my files. I am sorry. Could you expand a bit?

tankerkiller125's avatar

I believe that the correct Apache config should be

<VirtualHost *:80>    

    # The location of our projects public directory.
    DocumentRoot    /var/www/html/public    

    # Useful logs for debug.
    CustomLog       /var/www/html/access.log common
    ErrorLog        /var/www/html/error.log    

    # Rewrites for pretty URLs, better not to rely on .htaccess.
    <Directory /www/var/html>
        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On

        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)/$ /$1 [L,R=301]    

            # Handle Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>
    </Directory>    

</VirtualHost>

Some of the missing Rewrite Conditions are causing Apache not to rewrite the URLS correctly and thus larval isn't getting the correct information causing the 404 errors.

pemby's avatar
pemby
OP
Best Answer
Level 1

Changing my Apache config file to the below resolved the issue.

<VirtualHost *:80>
    ServerName somehost
    DocumentRoot /var/www/html/public    

    CustomLog       /var/www/html/access.log common
    ErrorLog        /var/www/html/error.log    

   <Directory /var/www/html/public>
      <IfModule mod_rewrite.c>
      Options -MultiViews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^ index.php [L]
   </IfModule>
</Directory>
</VirtualHost>    

I will try yours and report back. If yours works as well would you mind explaining the differences? I feel so lost with Apache and this would be a good learning opportunity. If yours works I will mark it as a solution because you found an answer before me :)

pemby's avatar

Ok I tested yours and it did not resolve the issue.
You seem to have a better understanding than me about what is happening here do you know why the one I posted works (laravel renders pages) ? If not no big deal. it WORKS!

henrique's avatar

@pemby A possible explanation is that .htaccess is not taking effect (you need "AllowOverride All" in apache's config for it to work). So with this, only apache's config is taking in consideration, so it tries to rewrite your request (for example http://domain.dev/test) to the file /var/www/html/index.php, which doesn't exist. When you added /public, it correctly found the file /var/www/html/public/index.php.

If you have any other question I may try to explain it to you :)

Please or to participate in this conversation.