Laravel 5 needs index.php using Apache virtual host

29,134

Solution 1

I ended up deleting the complete conf file and copied it again from the one thats working. I took the default .htaccess from laravel and it worked. Unfortunately I still dont know what was wrong though. The current conf file looks like

<Directory /var/www/mydomain.com>
    AllowOverride ALL
    DirectoryIndex index.php
</Directory>

My .htaccess in /public

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

Solution 2

This is the correct Alternative htaccess rule for Laravel 5 suggested to use when the default doesn't work:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

The Options +FollowSymLinks I believe plays an important role because the DirectotyIndex acts like a symlink.

Also check /app/config/app.php to make sure you haven't set the Config('app.url') to contain the index.php.

If you have laravel installed in a sub-directory see this: How can I remove "public/index.php" in the url generated laravel?

Solution 3

Have you tried turning it all off (shutdown), and then turning it on again?

If everything is as you say, it should work. I would try a restart to see if it changes anything.

If it doesn't, please update with what OS you are using.

Solution 4

I had almost exactly the same problem today, which @dasper also helped me out on. I am using digital ocean Ubuntu 14.04 also, and I had to run the command a2enmod rewrite then reload apache2. It seemed that even though rewrite appeared to be on, it actually wasn't.

Solution 5

The problem could be with your virtual host configuration and/or a misplaced .htaccess file.

Make sure your .htaccess file is in the public/ folder and that the DocumentRoot is set to that public/ folder.

And also, the < Directory> directive isn't controlling your virtual host but rather controls the folder itself and how it can be (or can't be) accessed on the server. To change the configuration of the virtual host do so inside the < VirtualHost> directive.

Here is an example of a virtual host configuration:

<VirtualHost *:80>
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot "/var/www/mydomain.com/public"
    DirectoryIndex index.php
</VirtualHost>

Here is the default Laravel Apache config:

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

and here is an alternative:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Share:
29,134
Sven van den Boogaart
Author by

Sven van den Boogaart

Software developer

Updated on July 28, 2022

Comments

  • Sven van den Boogaart
    Sven van den Boogaart almost 2 years

    When I load pages that are not root, the page won't load without index.php before the route. The error I get:

     Not Found
    
     The requested URL /login was not found on this server.
    
     Apache/2.4.7 (Ubuntu) Server at mydomain.com Port 80
    

    To start with I have a virtual host file containing:

    //also tried adding DirectoryIndex here before <directory>
    <directory /var/www/mydomain.com>
        DirectoryIndex index.php
        AllowOverride ALL
    </directory>
    

    and a .htacces in my public with :

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

    I have another domain with the same .htaccess and appache config on the same server and it works fine. Also I did restart apache. If I use phpinfo on my index.php/route page I see at Loaded Modules:

    mod_rewrite mod_setenvif 
    

    When running the website localy with xampp everything works fine. For hours i'm trying now but I can't fix it or find any error (logs are empty) or any solutions that I haven't tried yet.

    Edit:

    Im using Ubuntu Ubuntu 14.04 x64 on a digital ocean VPS. And I tried turning it on and off again (as suggested). PHP Version 5.5.9-1ubuntu4.9. I followed this tutorial to config everything (except the direcoty part). I changed the location of the apache log and a file error.log is created on the given directory but no errors are in it.

    My currently appache config is :this (i've triple checked the white parts where the domain name is).

    When I run apache2ctl -t D DUMP_VHOSTS I get

    enter image description here

    this looks fine to me, also tried disabling the default config but it didnt help.

    Note: i've replaced my real domain with mydomain.com in reality I use my real domain on these spots.

    Thought how do I know for sure that the conf file im editing is the one being used by the domain?