"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Host a Laravel Project in a Subdirectory on Shared Hosting without Exposing `/public` in the URL

How to Host a Laravel Project in a Subdirectory on Shared Hosting without Exposing `/public` in the URL

Published on 2024-11-17
Browse:106

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

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]

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.

Release Statement This article is reproduced at: https://dev.to/msnmongare/how-to-host-a-laravel-project-in-a-subdirectory-on-shared-hosting-without-exposing-public-in-the-url- 50cp?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3