0

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

Hans Meier
  • 33
  • 5
  • 1
    make your public folder as root of your host in apache.conf file, 000-default.conf if you are running linux – Divyank Feb 10 '17 at 11:16
  • I agree with @Divyank. Sometimes, host provider offers a back-office to manage your vhosts, so you can make your `domain.tld/public` to be read as `domain.tld/`. I personnaly use this last method as it is up to my host provider to handle the specific server file to perform this redirection. – Anwar Feb 10 '17 at 11:21
  • That is production app, full-online application available to administrative users. I can not change apache.conf file because i have my website on root location (www.mydomain.tld) – Hans Meier Feb 10 '17 at 11:26
  • okay i'll put one solution below after checking it on my local – Divyank Feb 10 '17 at 12:10
  • I asked slightly different version of the question here https://stackoverflow.com/questions/64305191/how-to-remove-public-from-laravel-url-hosted-on-child-folder – Talk is Cheap Show me Code Oct 11 '20 at 14:35

4 Answers4

3

In root folder make .htaccess file with:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Karem Shawky
  • 131
  • 1
  • 5
0

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';
Divyank
  • 740
  • 3
  • 20
0

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.

arku
  • 1,687
  • 17
  • 16
  • Didn't work for me, it said: `ln: failed to create symbolic link '/home/foobar/www/foo.bar.com/public_html/public': File exists` – Azamat Jan 26 '23 at 11:43
0

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]