Home > Article > Backend Development > How to Host a Laravel Project in a Subdirectory on Shared Hosting without Exposing `/public` in the URL
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.
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.
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.
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
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.
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.
The above is the detailed content of How to Host a Laravel Project in a Subdirectory on Shared Hosting without Exposing `/public` in the URL. For more information, please follow other related articles on the PHP Chinese website!