DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

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.

Step 1: Upload Your Laravel Project to the Server

  1. Log in to your hosting account and access your file manager.
  2. Navigate to the public_html folder or the main directory for your website.
  3. Create a new folder (subdirectory) for your Laravel project. In this example, we’ll name it hookbox-api.
  4. Upload your entire Laravel project to the hookbox-api folder.

Step 2: Move index.php to the Root of the Subdirectory

  1. Open the hookbox-api/public folder.
  2. Copy (or move) the index.php file from public to the root of hookbox-api.
  3. Open the copied index.php file in the root of hookbox-api and update the file paths as follows:
   require __DIR__.'/vendor/autoload.php';
   $app = require_once __DIR__.'/bootstrap/app.php';
Enter fullscreen mode Exit fullscreen mode

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

  1. Next, move the .htaccess file from the public folder to the root of hookbox-api.
  2. 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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 (0)