简体   繁体   中英

Remove public from URL Laravel 5.3 on shared hosting

I have Laravel 5.3 project on shared hosting located at the following address:

www.mydomain.tld/laravel/public/

How to remove "public" from URL?

www.mydomain.tld/laravel/.htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

www.mydomain.tld/laravel/public/.htaccess

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

In root folder make .htaccess file with:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

copy contents from your public folder and put them in your root folder

open index.php and modify following lines

require __DIR__.'/../bootstrap/autoload.php';

to

require __DIR__.'path_to_laravel_folder/bootstrap/autoload.php';

&

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

to

$app = require_once __DIR__.'path_to_laravel_folder/bootstrap/app.php';

The best way for this situation is using symlinks. Login to your webserver via SSH. Change your directory to your website. Change it to parent. Create a symlink with ln command. Replace public_html with your foldername (it could be kinda like your website name or www folder).

ln -s public /your/full/path/public_html (use pwd to get full path)

if SSH is not available for your hosting and no different way to create symlink or edit the apache.conf file I would suggest you to change hosting to another one. This is very bad practice to make entire application available to world.

In first step it is very easy and you need to just rename file name. you have to rename server.php to index.php at your laravel root directory.

Second l you have to copy .htaccess file and put it laravel root folder. You just copy .htaccess file from public folder and then update bellow code:

Options -MultiViews -Indexes

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_URI} !(.css|.js|.png|.jpg|.gif|robots.txt)$ [NC]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} !^/public/

RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]

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