简体   繁体   中英

Laravel 5.4 - Routes not working

I've setup a fresh ubuntu mini installation and installed apache2 and PHP. (also tried it with latest stable ubuntu) I've also installed some moduls, amongst others the moduls listed in the laravel installation documentation :

OpenSSL PHP Extension
PDO PHP Extension
Tokenizer PHP Extension
Mbstring PHP Extension
XML PHP Extension

I also installed:

libapache2-mod-php , git and curl, php-curl :

sudo apt-get install libapache2-mod-php
sudo apt-get install curl
sudo apt-get install php-curl
sudo apt-get install git

Now I copied a whole laravel project from my apache server from windows. Then I executed

chmod 775 -R *

and

chown www-data:www-data -R *

However, the routes are not working.

eg http://192.168.2.100/selenium/public/start

Should show my start page, instead I get:

Not Found: The requested URL /selenium/public/start was not found on this server.`

So I researched and found this on stackoverflow.

From the answer:

 Try to navigate to your expected route prepend it with /index.php/, in your case: http://localhost/laravel/index.php/users. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps. 

So I tried to call my page like suggested:

http://192.168.2.100/selenium/public/index.php/start

That worked. So there is a problem with the Rewrite Module Configuration.

He/She suggested to add RewriteBase /directory/ under the line RewriteEngine On in public/.htaccess

So I changed my public/.htaccess to this and restarted apache2:

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

    RewriteEngine On
    RewriteBase /selenium/

    # 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]

    # Handle Authorization Header

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

However, the routes are still not working. What else can I try?

System Infos

Apache version:

 Server version: Apache/2.4.7 (Ubuntu) Server built: Jul 15 2016 15:32:47 

PHP version:

PHP 7.0.16-4+deb.sury.org~trusty+1 (cli) (built: Mar  2 2017 13:50:00) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.16-4+deb.sury.org~trusty+1, Copyright (c) 1999-2017, by Zend Technologies

Linux version:

Linux linux 3.13.0-110-generic #157-Ubuntu SMP Mon Feb 20 11:55:25 UTC 2017 i686 i686 i686 GNU/Linux

Many a times if its a fresh install of Ubuntu and you install apache, the rewrite module is not enabled by default. Run the following in your terminal

sudo a2enmod rewrite

sudo service apache2 restart  

Now since the rewrite module is enabled, the .htaccess from your laravel project's public folder will be read and routes will work as intended.

I did not have to change any line in the default .htaccess file in laravel.
Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM