简体   繁体   中英

How do I get URL rewrite to work in Laravel 5?

I cannot manage to get any Laravel routing to work except the base URL and the files in the /site/public/ folder.

http://testurl/ (set through an apache vhost) works, and loads the home page.

http://testurl/blog ) doesn't work (returns 404), but I would like it to.

Strangely, http://testurl/index.php/blog does work.

I have Googled in a hundred different places and looked at uncountably many StackOverflow questions and have tried everything that I could find, but nothing worked.

How can I fix this?


Configuration info

I am running Laravel 5.7 on Apache 2.4.29 and PHP 7.1.9 on MacOSX 10.13.4.

I have enabled the rewrite module in httpd.conf like so:

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
...
<Directory />
    AllowOverride all
    Require all denied
</Directory>

My httpd-vhosts.conf is set up like so:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents/site/public"
    ServerName testurl
    <Directory /Library/WebServer/Documents/site/public>
            Order Allow,Deny
            Allow from all
    </Directory>
</VirtualHost>

The .htaccess in the /site/public/ folder looks like this (I'd rather not have to change this):

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

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

My config/app.php has the following line:

'url' => env('APP_URL', 'http://testurl'),

And last but not least, web.php looks like this:

<?php

Route::get('/', function () {
    return view('home');
});
Route::get('blog', function () {
    return view('blog');
}); 

If there is anything else that I am missing (or anything that I can try), please let me know.

Preferably, I would like solutions that won't require me to change my Laravel code when deploying to a server.

Thanks for the help!

After a LOT of searching I found this question and it helped me find the answer.

All I needed to do was add AllowOverride All to my httpd-vhosts.conf file like this:

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents/site/public"
    ServerName testurl
    <Directory /Library/WebServer/Documents/site/public>
            AllowOverride All
            Order Allow,Deny
            Allow from all
    </Directory>
</VirtualHost>

This corrected the issue, and now everything loads properly.

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