Search code examples
phplaravel.htaccesslaravel-5laravel-artisan

Laravel - How to remove index.php from Laravel in windows xampp


Problem:

my routes not working except the root home page, I'm searching for two days to find a solution to this problem and what I found that I should change .htaccess file but solutions didn't fix any for my case, at first the url localhost/quotes/public was working well with me, but at some point I'm not sure what is it this issue showed up

what I tried:

  • create another route and I made sure that no routes are working only home route, still not working except home
  • tried to change OverrideMode on my XAMP from None to All, didn't fix any
  • tried to type manually localhost/quotes/public/index.php BOOM everything works ..

my htaccess file:

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

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

working on:

  • Windows 10
  • XAMP
  • Laravel 5.2.35

Solution

  • The problem is that your .htaccess is rewriting everything to the frontcontroller, which is normally located at {host}/index.php. In your application however it is located at {host}/quotes/public/index.php.

    So you have 2 options:

    1. virtual host
    Set up a virtual host in your XAMPP Apache that points ie. myapp.local to htdocs/quotes/public Here is an example of how to achieve this: how to create virtual host on XAMPP. (Don't forget to add the host to your hosts file and have it point to your local macine on 127.0.0.1) You can then access your application on myapp.local/whatever-route-you-define. Alternatively you forget about XAMMP and install the homestead virtual machine, which comes preconfigured for this.

    2. rewrite rule
    Change you rewrite rule to rewrite all requests to quotes/public/index.php in stead of index.php. I'm no htaccess expert, but I believe it should be as simple as changing this:

    RewriteRule ^ index.php [L]
    

    to this:

    RewriteRule ^ quotes/public/index.php [L]
    

    Do note that you'll still need to access your application trough localhost/quotes/public/whatever-route-you-define which is not ideal imo. Your dev version should be as close to your live version as possible, and if you start working with absolute and relative paths and stuff in your code things will become a mess sooner rather then later.


    Personally I would go for Homestead, I use it all the time and it works great once you have it running.

    Btw, the reason why localhost/quotes/public/index.php is working for you right now is because RewriteCond %{REQUEST_FILENAME} !-f tells Apache not to rewrite any requests to files that actually exist (otherwise you wouldn't be able to access static assets like your css).