24

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?

** Note the question marked as duplicate does not have the problem and answer that solved this question, the only similair thing is that it is about a htaccess file.

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

9 Answers9

12

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?

Neo
  • 11,078
  • 2
  • 68
  • 79
8

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.

CenterOrbit
  • 6,446
  • 1
  • 28
  • 34
6

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>
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
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.

Ben Harvey
  • 1,793
  • 2
  • 16
  • 23
  • this answer worked for me, tried rolling my own lamp stack and put my code in var/www/html and this is the only one that works for me. took me hours to find the docs here [laravel-docs](https://laravel.com/docs/5.0/installation#pretty-urls) – Spurdow Jan 27 '16 at 16:44
3

First, make sure mode_rewrite is enabled. I am sure you have probably done this but I want to make sure nothing is missed.

Next, see what version of apache you are running since the syntax has changed between 2.2 and 2.4. This should not be a big deal and highly doubt this is the culprit but could save you trouble in the future

Next, try putting the rewrite condition inside of the vhost information instead of the htaccess file. This can help a little with performance as well as give you a console warn/err when restarting apache where the htaccess errors could be squelched.

<directory /var/www/mydomain.com>
    Options -MultiViews
    AllowOverride ALL
    RewriteEngine On

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

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</directory>

Beyond this point I would be at a loss without some more logging or information from the server. You can also add CustomLog into the vhost and report all requests processed by the server.

dasper
  • 692
  • 4
  • 13
3

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]
Jocke Med Kniven
  • 3,909
  • 1
  • 22
  • 23
2

Try to rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder.

Solution found here

Community
  • 1
  • 1
Alex Kyriakidis
  • 2,861
  • 1
  • 19
  • 28
2

I believe your directory path is wrong. You need /public on the end because Laravel treats that directory as the web root. Try this:

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

But the better solution is to put the contents of the .htaccess file in your virtual host. Like this:

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

    <Directory "/var/www/mydomain.com/public">
        # Ignore the .htaccess file in this directory
        AllowOverride None

        # Make pretty URLs
        <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>
    </Directory>
</VirtualHost>

Also, you should make sure your config file is loaded by running:

apache2ctl -t -D DUMP_VHOSTS

This will print a list of all the loaded vhosts. If yours isn't in that list then make sure it is enabled by running:

a2ensite [name_of_your_config_file]

Then restart apache with:

service apache2 reload

BrokenBinary
  • 7,731
  • 3
  • 43
  • 54
2

One more note: don't forget to edit /etc/hosts and add 127.0.0.2 yourdomain.com

javier_domenech
  • 5,995
  • 6
  • 37
  • 59