Home >Backend Development >Python Tutorial >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

Barbara Streisand
Barbara StreisandOriginal
2024-11-07 20:07:031102browse

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.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn