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

最后都变了- 提交于 2019-12-14 02:59:25

问题


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

回答1:


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




回答2:


The .htaccess file must be at the root of the application. Add this in this file :

RewriteEngine On

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

Assuming you haven't touched the original architecture of Laravel, and that public data is still in the same place : the public/ folder

You can also follow this good tutorial




回答3:


Let me give you an example of the way I have my routes setup.

In app\Http\routes.php, here are three sample routes that I have.

Route::get('/', function () {
    $values = app('App\Http\Controllers\KeywordController')->index();
    dd($values);
    return view('welcome');
});

Route::get('googlefile', function () {
    $output = app('App\Http\Controllers\KeywordController')->printToFileGoogle();
    dd($output);
});

Route::get('bingfile', function () {
    $output = app('App\Http\Controllers\KeywordController')->printToFileBing();
    dd($output);
});

I have WAMP setup on my environment. I have made a controller at app\Http\Controllers\KeywordController.php. If my browser is set to localhost/googlefile, then it will goto the method printToFileGoogle() in KeywordController.php.

Please try something similar to this and tell me if you get an error and if you do what error you get.



来源:https://stackoverflow.com/questions/37686003/laravel-how-to-remove-index-php-from-laravel-in-windows-xampp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!