-->

Laravel 4 routing not working due to .htaccess fil

2019-07-18 22:16发布

问题:

I'm writing my steps and findings here so you can see what I've tried and what results I got. Any advice would be welcomed. I followed the comments in this answer.

I'm running Laravel 4, using XAMMP version 1.8.2 with PHP 5.4.19 and Apache 2.4.4 on a Windows 7 machine and I'm simply still trying to get a local instance up and running.

In my case: http://localhost/sos/sos_public/ is my main screen and that works, but when I try to get to http://localhost/sos/sos_public/signup I get this error: Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException which has been talked about a lot on the net, and then I found GaryJ stating that it might be an .htaccess issue, so I did what he suggested.

My /app/routes.php file looks like this (I've tried this with a / in front of signup too):

Route::get('signup', function()
{
    return 'hello!';
});     

Route::get('/', function()
{
    return View::make('hello');
});

First:

Just for a laugh, see if /index.php/hello works. If so, then it's a .htaccess problem.

http://localhost/sos/sos_public/index.php/signup worked perfectly fine. So it's an .htaccess problem.

My .htaccess file looked like this:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

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

And:

if you're running Apache 2.4, note the changes since previous versions, regarding Require all granted and AllowOverride all within a <Directory />...</Directory> block on your virtual host.

I added this in my httpd.conf file as suggested by Dalton Gore - got the Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException error (changed this directory path to /SOS/sos_public and C:/xammp/htdocs/SOS/sos_public/ and C:/xammp/htdocs/SOS/sos_public - same results):

<Directory />
    AllowOverride none
    Require all denied
</Directory>
<Directory /SOS/sos_public>
    AllowOverride all
    Require all granted
</Directory>

Next:

Check if anything in .htaccess is working

I added dsjkdsfghk to my .htaccess file and immediately got an error, so I know my .htaccess file is being used.

Then:

Try removing the IfModule conditional. As you've got access to the host / vhost, you can soon enable that module if it's not - so it doesn't need be checked on every request. Equally, try moving it out of .htaccess, and into a <Directory />...</Directory> block in your vhost - if you've got nothing else in your .htaccess it can then be deleted as well.

  • Having an empty .htaccess file caused a 404 error in my browser for http://localhost/sos/sos_public/signup (http://localhost/sos/sos_public/ still worked).
  • Removing the .htaccess file from C:\xampp\htdocs\SOS\sos_public\ had the same results.

Then:

Try: <VirtualHost *:80> DocumentRoot "/Users/amiterandole/Sites/laravelbackbone/public" ServerName laravelbackbone.dev <Directory /> AllowOverride all Require all granted </Directory> <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule> </VirtualHost>

I did that and just like Amit,

I emptied out my htaccess file and tried the above and now nothing seems to work.

Except where his laravelbackbone.dev pointed to his Sites folder root, I just got an Error 400 - Bad request on both http://localhost/sos/sos_public/ and http://localhost/sos/sos_public/signup when I ran them in my browser.

My httpd-vhosts.conf file looked like this:

<VirtualHost *:80> 
    DocumentRoot "C:/xammp/htdocs/SOS/sos_public" 
    ServerName sos.dev
    <Directory /> 
        AllowOverride all 
        Require all granted 
    </Directory> 
    <IfModule mod_rewrite.c> 
        Options -MultiViews 
        RewriteEngine On 
        RewriteCond %{REQUEST_FILENAME} !-f 
        RewriteRule ^ index.php [L] 
    </IfModule> 
</VirtualHost>

and in my .hosts file I obviously had:

127.0.0.1       sos.dev

Lastly:

You've definitely got the conf/extra/httpd-vhosts.conf file be included (uncommented) within the main httpd.conf?

I have this uncommented - yes.

Another link I tried but to no avail: https://stackoverflow.com/a/17778222/956975 and http://www.epigroove.com/blog/laravel-routes-not-working-make-sure-htaccess-is-working

What should I change where? What am I missing?

回答1:

Your ServerName is

sos.dev

And apache take this in consideration, you you must access your routes using:

http://sos.dev/

And NOT

http://localhost/