Lamar
Lamar

Reputation: 1839

Removing /public from URL in Laravel 7

Even though there are hundreds similar questions out there, but none is actually solving my problem. I have setup a Laravel 7.14.1 application, and I uploaded it to a shared host.

To make it work on shared host, I did the following steps:

1- Renamed server.php to index.php

2- Moved .htaccess to root

3- In .htaccess I put the following rules

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]

4- Created a test route like follows:

Route::get('/test', function () {
    return 'HI';
});

Now when I visit domain.com/test it gives me 404 error But when I visit doamin.com/public/test it works

How can I remove the /public part from my url?

Upvotes: -1

Views: 1554

Answers (3)

ashish
ashish

Reputation: 3900

Easy way to resolve this.

  1. First, upload the contents of public folder on your local machine to public_html on web server.
  2. upload every other files and folders outside public_html.
  3. and done, no configuration files update necessary.

Let me know how it goes

Upvotes: 1

araujophillips
araujophillips

Reputation: 342

The simplest and safest solution for shared hostings:

  1. Go to your Laravel/Lumen root path from SSH
  2. Run mv .htaccess .htaccess-BACKUP to backup your current .htaccess
  3. Run cp public/index.php ./ && cp public/.htaccess ./
  4. Enjoy :)

NOTE: If you don't have SSH access, you can do exactly the same thing manually from cPanel > File Manager

Upvotes: 0

Izweb Technologies
Izweb Technologies

Reputation: 158

The following are the steps you can follow to host your laravel application on shared hosting(cpanel)

1 Step: Login to your shared hosting account (cpanel) dashboard then find File Manager icon then click it

enter image description here

enter image description here

Click public_html , actually this is where normally we upload our web application contents, so upload your laravel project here but we need to do extract work to make our project running please proceed with following steps

2. Step: Organize your laravel project

enter image description here

Create folder in main root of your laravel project any name will fit, mine i call it system as shown below enter image description here

Then i move all the other folders except public to this new created folder.

enter image description here

Here you will result into

enter image description here

Takes all your public content outside the main root as shown below

enter image description here

Then here what will result

enter image description here

You can remove public folder or leave it

3 Step: Edit index.php file found in your main root

enter image description here

We need to edit line code number 22 and 36 to the following as shown below

NB: The secret behind is to tell our index.php file where it can find those two files

You can test your project by navigating to your domain.

enter image description here

4. Step : Conclusion,

There is interesting package that sum up all what we did as laravel package please have a look, click here

Upvotes: 0

Related Questions