Laravel routes return 500 error

10,611

Solution 1

For those who just reading this through, and still have no solution, it could be that you just cloned a project from a repo on your gitlab or github account and forgot to generate the .env file and APP_KEY init. so go to your project directory and open a terminal and run cp .env.example .env. this would create the .env file. then run php artisan key:generate.

just note that in my special case laravel returned 500 error for every route.

Solution 2

Solution1:

add '/' before all your routes; your case '/hello'. Or ;

Solution2:

I guess .htaccess is ignored. http://httpd.apache.org/docs/2.2/en/mod/core.html#allowoverride

*apache2.conf file in /etc/apache2 : *

ServerName localhost

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

Options Indexes FollowSymLinks AllowOverride None Require all granted

Solution 3:

Apache may be configured to deny .htaccess overrides. In that case you'll need to add a segment in your VirtualHost configuration allowing those. See the Apache documentation for more information. It also may be the case that mod_rewrite is not enabled. If using Ubuntu, Debian or other Debian-based OS is used a sudo a2enmod rewrite followed by sudo service apache2 reload will suffice.

Here is mine and it works

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

    RewriteEngine On
    RewriteBase /laravel51/public/
   # change above to your site i.e.,  RewriteBase /whatever/public/

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Share:
10,611
Billy Ray
Author by

Billy Ray

Updated on June 04, 2022

Comments

  • Billy Ray
    Billy Ray almost 2 years

    I deploy basic Laravel project to server. When I hit my domain it returns default welcome view. When I add simple road (see below) to code and try to enter that route in browser it returns 500 internal error. All routes return 500 error except the "/" root route.

    Folder structure:

    /
    #laravel
    #subdoms
    ##api
    

    Laravel files are in laravel directory except files from public directory which are in api diretory.

    .htaccess file in api directory:

    <IfModule mod_rewrite.c>
      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]
    
      # Handle Authorization Header
      RewriteCond %{HTTP:Authorization} .
      RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    </IfModule>
    

    Storage directory and everything in it is writeable, readable, executable for anyone.

    There are no error logs in laravel/storage/logs.

    laravel/routes/web.php:

    <?php
    
    Route::get('/', function () { // works fine.
        return view('welcome');
    });
    
    Route::get('hello', function () { // 500 internal error
        return 'Hello world';
    });
    

    Server info:

    Server - Linux CentOS - Apache 2.2 - Server Side Includes - SSI - PHP Version 7.0.17