When hosting a Laravel project on shared hosting, one common challenge is ensuring that URLs don't require the /public
directory. Here’s a step-by-step guide to hosting your Laravel app in a subdirectory while keeping URLs clean.
Step 1: Upload Your Laravel Project to the Server
- Log in to your hosting account and access your file manager.
- Navigate to the
public_html
folder or the main directory for your website. - Create a new folder (subdirectory) for your Laravel project. In this example, we’ll name it
hookbox-api
. - Upload your entire Laravel project to the
hookbox-api
folder.
Step 2: Move index.php
to the Root of the Subdirectory
- Open the
hookbox-api/public
folder. - Copy (or move) the
index.php
file frompublic
to the root ofhookbox-api
. - Open the copied
index.php
file in the root ofhookbox-api
and update the file paths as follows:
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
This tells Laravel to find the necessary files within the project’s root instead of public
.
Step 3: Move the .htaccess
File to the Root of the Subdirectory
- Next, move the
.htaccess
file from thepublic
folder to the root ofhookbox-api
. - Replace the contents of this
.htaccess
file with the following:
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This code ensures that all incoming requests are directed to index.php
in the hookbox-api
folder. It also forces HTTPS if your site has SSL enabled.
Step 4: Clear Laravel’s Cache (Optional but Recommended)
If you have SSH access, clearing cached configuration and routes is always a good practice after any deployment changes. Run these commands to ensure no cached configuration conflicts remain:
php artisan route:cache
php artisan config:cache
php artisan cache:clear
Step 5: Test Your Setup
Now, you should be able to access your Laravel application in the browser without needing /public
in the URL. Try accessing a route like:
https://www.yourdomain.com/hookbox-api/api/your-route
If the setup was successful, this should load without any errors.
Conclusion
By moving index.php
and .htaccess
to the root of your subdirectory and updating the file paths, you’ve effectively configured Laravel to run without exposing the /public
directory in the URL. This method is useful when working with shared hosting, as it maintains a cleaner, more professional URL structure.
Top comments (1)
Great tutorial for setting up Laravel on shared hosting! I remember struggling with exposing the /public directory in the URL when I was using shared hosting. Since I moved to Cloudways, I don’t have to deal with these manual configurations anymore. Cloudways' platform makes deploying Laravel apps so much easier with pre-configured servers and staging environments. Highly recommend it to anyone looking for a smoother hosting experience!