Laravel Shared Hosting .htaccess

Laravel Shared Hosting .htaccess

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing Laravel Routing on Shared Hosting Without the /public Directory

Deploying a modern PHP framework like Laravel onto shared hosting often hits a specific wall: the inability to strip the /public directory from your URL structure. While you might successfully access www.mywebsite.com/test/public/ or even point directly to www.mywebsite.com/test/index.php, attempting to hit www.mywebsite.com/test/ results in a standard Apache 403 Forbidden error.

This behavior stems from how the .htaccess file interacts with the document root and the specific constraints of shared hosting environments. The goal is clear: serve all requests directly to the application's entry point (index.php) without requiring the user to specify the public/ subdirectory, while ensuring that static assets (CSS, JS, images) located outside the public folder are not inadvertently exposed or blocked incorrectly.

Why the Default Rule Fails on Shared Hosting

Your current configuration attempts to rewrite all requests to public/index.php:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ public/index.php [L]
</IfModule>

On a local development environment or VPS where you have full control over the file system, this works perfectly. However, shared hosting providers often enforce a specific document root structure. If your application's public directory is located at /home/user/test/public/, but the server's document root for the domain test is set to /home/user/test/, Apache sees two conflicting realities:

  1. The Document Root: The server expects files to live directly in /home/user/test/.
  2. Your Rewrite Rule: You are telling Apache to serve everything from /home/user/test/public/index.php.

When you request www.mywebsite.com/test/, the URL path maps to /home/user/test/ on the server. Because your rule rewrites this to public/index.php, Apache attempts to serve a file located at /home/user/test/public/index.php as if it were /home/user/test/index.php. If the document root is strictly enforced or if mod_rewrite behaves differently in that specific context, the server may return 403 Forbidden because it cannot resolve the requested path relative to the actual document root.

The Solution: Adjusting the Document Root or Rewrite Path

To fix this without migrating to a VPS, you must align your .htaccess rules with the physical file structure enforced by the hosting provider.

Option 1: Keep the /public Directory in URL (Recommended for Shared Hosting)

The most robust solution on shared hosting is often to accept that the document root is the public folder. If you cannot change the document root setting via cPanel or Plesk, your .htaccess should route requests to the public folder's contents, not bypass the folder structure entirely in a way that conflicts with the server config.

However, if your goal is to access the site via www.mywebsite.com/test/ and the files physically live in public/, you need to ensure the rewrite rule points to the correct physical path relative to the current directory.

If your document root in cPanel points to /home/user/test/public/, your .htaccess inside that folder should look like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # If the file or directory does not exist, try rewriting to index.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [L]
</IfModule>

# Handle static assets served directly by the server (optional but recommended)
<IfModule mod_mime.c>
    <FilesMatch "\.(css|js|jpg|jpeg|png|gif|ico|svg)$">
        RewriteRule ^ - [E=HTTP_AUTHORIZATION:REMOVED]
    </FilesMatch>
</IfModule>

# Ensure Laravel's public assets are accessible
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    
    RewriteEngine On
    RewriteRule ^\.(?!\.)$ - [F,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

Option 2: Change the Document Root (If Possible)

If you have access to cPanel or Plesk, check if you can set the "Document Root" for the domain test directly to /home/user/test/public/. Once that is configured in the hosting control panel, your .htaccess file located at /home/user/test/public/ can use the simpler standard Laravel rule:

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Route all requests to index.php within this directory
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

In this scenario, www.mywebsite.com/test/ maps directly to the physical path of your public/index.php, eliminating the need for a relative path rewrite that causes 403 errors.

Critical Considerations for Shared Environments

When deploying on shared hosting, you must be wary of directory permissions and symlink handling. Laravel's default structure often relies on symlinks to separate storage and config folders. Ensure that your .htaccess does not inadvertently block access to public/storage or public/bootstrap/cache.

A common pitfall is assuming that the standard Laravel .htaccess template works verbatim without checking the document root settings in your control panel. If the server's document root is set to the parent directory (/home/user/test/) but you are rewriting requests to a subdirectory (public/index.php), Apache will throw a 403 error because it cannot find index.php at the root level, only at the nested public level.

If you encounter persistent issues where static assets fail to load even after fixing the routing, verify that mod_mime is enabled and that your hosting provider isn't stripping headers required for asset serving. In some restrictive shared environments, it may be necessary to serve static files outside of Laravel's rewrite rules entirely, though this requires careful configuration to avoid security vulnerabilities.

For complex deployments where .htaccess limitations become too restrictive, migrating to a managed VPS or using a provider that supports containerized deployments (like Docker) often resolves these structural conflicts automatically.

Our network