Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

youssefy's avatar

Redirect requests to the "public/" directory using .htaccess while the project in a subdirectory

Hello, I have a little bit complicated situation here, my directory structure looks like this:

/
├─ index.html
├─ .htaccess
├── backend/
│   ├── public/
│   │   ├── index.php

My goal is to set up the .htaccess file in the root directory so that any URL like example.com/backend is redirected to example.com/backend/public or in another word example.com/backend/index.php is redirected to example.com/backend/public/index.php. The backend directory contains a Laravel project that serves as an API, and I need to access it via url like example.com/backend/anylaravelroute.

Here is my current .htaccess file:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    
    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /backend/public/index.php [L]
</IfModule>

The issue I'm facing is that while this .htaccess configuration successfully redirects requests to /backend/public/index.php, the $_SERVER['REQUEST_URI'] in PHP still contain the path with /backend/. As a result, Laravel handles routes incorrectly and returns a 404 error. For example, example.com/backend/api returns a 404 error, while example.com/api works but not what i want, the APP_URL in laravel is set to example.com/backend

Unfortunately, I cannot change the directory structure by moving the public directory outside of /backend/ due to specific constraints, and i don't have control over apache configuration, also solving this will prevent any direct access to files in backend directory because all requests will redirected to backend/public so no security problem.

I tried a lot of different solutions without success, I would appreciate any assistance or guidance on .htaccess configuration to achieve the desired behavior.

0 likes
1 reply
jlrdw's avatar

I suggest setting the folder structure up correctly and pointing to public as the document root.

I generally place main laravel out of web.

FYI there are some good previous post of how to properly set all of this up. Both myself and others have given examples.

1 like

Please or to participate in this conversation.