简体   繁体   中英

Laravel remove public from url

This is my folder structure for Laravel 5.2

-laravel
--app
--bootstrap
--config
...

Under the laravel folder that is root of Laravel, I added an .htaccess file with following code:

RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

Now when I open project in browser I get the error:

Sorry, the page you are looking for could not be found. "NotFoundHttpException in RouteCollection.php line 161:"

I am using XAMPP. Why I am getting this error? How can I remove public from the URL using this method?

You need to point your web server to a public directory instead of project root directory. Use original .htaccess and these Apache settings:

DocumentRoot "C:/xampp/htdocs/public"
<Directory "C:/xampp/htdocs/public">

Restart Apache to make it work.

The best way is to create a virtual host in xampp and set its DocumentRoot to laravel public folder. Here is a nice tutorial on how to create vhosts in xampp

https://delanomaloney.com/2013/07/10/how-to-set-up-virtual-hosts-using-xampp/

create a folder and name as you want, move all the files inside that folder except public, move the content of public folder to root. After you must edit index.php and change [newFolder] by your news folders name

require __DIR__.'/[newFolder]/bootstrap/autoload.php';
$app = require_once __DIR__.'/[newFolder]/bootstrap/app.php';

•Go to mainproject/public>>

a.  .htacess
b.  favicon.ico
c.  index.php
d.  robots.txt
e.  web.config

1.cut these 5 files from public folder,and then paste on the main project folder that's means out side of public folder… mainproject/files

2.Next after paste ,open index.php ,modify

•require __DIR__.'/…/bootstrap/autoload.php';  to
•require __DIR__.'/bootstrap/autoload.php';

    modify

    $app = require_once DIR.'/../bootstrap/app.php'; to

    $app = require_once DIR.'/bootstrap/app.php';

Change your httpd.conf file from

DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">

to

DocumentRoot "C:/xampp/htdocs/{projectname}/public"
<Directory "C:/xampp/htdocs/{projectname}/public">

restart apache. then you can now access your project in url localhost/

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