19

I am new to laravel. I am using Ubuntu 15.04. I installed Laravel Framework version 5.1.7 (LTS) using composer and a lamp server using $ sudo apt-get install lamp-server^ command (I didn't install Homestead). I am using PhpStorm 8.0.3 as IDE.

I created three routes and a controller. The PagesController.php file looks like this:

class PagesController extends Controller
{
    public function index()
    {
        return 'Welcome to my homepage!';
    }

    public function about()
    {
        return 'Learn a little about me.';
    }

    public function hello()
    {
        return 'Hello World!';
    }
}

and the routes.php looks like this:

Route::get('/', 'PagesController@index');

Route::get('about', 'PagesController@about');

Route::get('hello', 'PagesController@hello');

Whenever I go to http://localhost:63342/my-first-app/public/ (or http://localhost:63342/my-first-app/public/index.php) it works fine and shows me the Welcome to my homepage! message. But whenever I go to http://localhost:63342/my-first-app/public/hello or http://localhost:63342/my-first-app/public/about, what I get is 404 Not Found message.

Also, the .htaccess file which is located at my-first-app/public looks like this:

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

What I have tried:

  • I tried http://localhost:63342/my-first-app/public/index.php/hello or http://localhost:63342/my-first-app/public/index.php/about but it doesn't work either.
  • I entered command sudo a2enmod rewrite followed by sudo service apache2 restart but it doesn't work either.
  • I tried composer dump-autoload but it doesn't work either.
  • I changed AllowOverride from None to All in apache2.conf. Now part of it looks like this:

    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all denied
    </Directory>
    
    <Directory /usr/share>
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    <Directory /srv/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    

    but it doesn't solve the problem either.

Update (7/15/2015):

The result of running php artisan route:list looks like this:

+--------+----------+-------+------+--------------------------------------------+------------+
| Domain | Method   | URI   | Name | Action                                     | Middleware |
+--------+----------+-------+------+--------------------------------------------+------------+
|        | GET|HEAD | /     |      | App\Http\Controllers\PagesController@index |            |
|        | GET|HEAD | about |      | App\Http\Controllers\PagesController@about |            |
|        | GET|HEAD | hello |      | App\Http\Controllers\PagesController@hello |            |
+--------+----------+-------+------+--------------------------------------------+------------+
15
  • Run "php artisan route:list" and paste the output. You should see all defined routes there. Jul 15, 2015 at 17:37
  • @jedrzej.kurylo Thanks for your reply. I added the result at the end of my question.
    – today
    Jul 15, 2015 at 18:12
  • Could you add dd('test'); at the top of your index file, go to "localhost:63342/my-first-app/public/index.php/about" and see if test will be output? this way we'd know if it's something with rewrites or with Laravel code Jul 15, 2015 at 18:58
  • @jedrzej.kurylo I added dd('test'); at the top of index.php file. Now, when I go to http://localhost:63342/my-first-app/public/index.php it shows me a blank page and when I go to http://localhost:63342/my-first-app/public/index.php/about it gives me the same 404 Not Found message.
    – today
    Jul 15, 2015 at 19:17
  • 1
    Would you please run php artisan serve --port 8000 --host 127.0.0.1 in your consol (maybe terminal since you are in Linux) and then navigate to http://localhost:8000/about and tell us what happens? Also, remember you may have to start your apache server on your lampp. Jul 17, 2015 at 18:43

6 Answers 6

11

Enable apache2 rewrite module:

sudo a2enmod rewrite

Then restart the apache2 server:

sudo service apache2 restart

And make sure that AllowOverride All to your apache2 config file.

1
  • This is the simplest way for linux. It works for me. Thanks Jan 28, 2019 at 6:14
11

I have been having this annoying problem for a long time. Open .htaccess file in public folder and replace the following code, make a backup of your original code just in case.

<IfModule mod_rewrite.c>

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

    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/index.php [L]

</IfModule>
1
  • Thanks a lot for sharing this solution.. it works for me. Jan 12, 2021 at 22:08
9

Step 1: Enable mod_rewrite

sudo a2enmod rewrite

Step 2: To activate these changes restart Apache2

sudo service apache2 restart

Now check your web page is it working or not. If not follow Step 3.

Step 3: Edit Apache Configuration file

To Edit run below command:

sudo nano /etc/apache2/sites-available/000-default.conf

Then add following block of code below <VirtualHost *:80>

<Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
</Directory>

Save the file and exit.

Restart apache2:

sudo service apache2 restart

It should be working fine now.

1
  • I updated your answer to include ReWriteEngine On just for good measure
    – TimD
    Mar 10, 2020 at 12:42
2

Altering .htaccess file would work in this scenario per laravel 5 official document as below:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
2

Most of the times Beginners do not create .htaccess file in their project please make sure to copy .htaccess file from public folder and paste it on your project's root directory...

0

For Debian 11, Laravel Version:8.83.10

I had similar problem.

pico /etc/apache2/apache2.conf

followed by changing following block solved problem

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

putting / at start, end of route is not important

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.