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

5
  • 1
    make your public folder as root of your host in apache.conf file, 000-default.conf if you are running linux Commented Feb 10, 2017 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. Commented Feb 10, 2017 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) Commented Feb 10, 2017 at 11:26
  • okay i'll put one solution below after checking it on my local Commented Feb 10, 2017 at 12:10
  • I asked slightly different version of the question here stackoverflow.com/questions/64305191/… Commented Oct 11, 2020 at 14:35

4 Answers 4

5

In root folder make .htaccess file with:

<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule> 
Sign up to request clarification or add additional context in comments.

Comments

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'; 

Comments

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.

1 Comment

Didn't work for me, it said: ln: failed to create symbolic link '/home/foobar/www/foo.bar.com/public_html/public': File exists
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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.