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

BuenosAires's avatar

.htaccess error when trying to redirect to public folder on deployment

Hi, i followed a guide on how to setup an .htaccess to redirect from root to public folder on a server, after creating .htaccess on root, tweaking the code because all the project is inside a subfolder and trying it on XAMMP, every route works like its suppoused to like testwebsite.test/login , but if i try to enter testwebsite.test/ the URI changes to testwebsite.test/subfolder_name/public/subfolder_name/public/.

It still works and properly shows '/' route, but i dont know why the URI change happens and if i try to refresh the page it throws a 404 as there is no /subfolder_name/public/subfolder_name/public/ route, obviously.

My guess is that the .htaccess on /public doesnt know how to handle if its a redirect from another .htaccess but i dont have much knowledge of this subject so i can't solve it on my own thats why im asking for help.

<IfModule mod_rewrite.c>
# That was ONLY to protect you from 500 errors
# if your server did not have mod_rewrite enabled

RewriteEngine On
# RewriteBase /
# NOT needed unless you're using mod_alias to redirect

RewriteCond %{REQUEST_URI} !/subfolder_name/public
RewriteRule ^(.*)$ subfolder_name/public/$ 1  [L] // 1 is next to $ in code but laracasts hides them if they are together for some reason
# Direct all requests to /public folder

</IfModule>

(the whole projects needs to be on subfolder_name subfolder so it isnt public, the original code on the guide was RewriteCond %{REQUEST_URI} !/public instead of RewriteCond %{REQUEST_URI} !/subfolder_name/public and RewriteRule ^(.*)$ public/$1 [L] instead of RewriteRule ^(.*)$ subfolder_name/public/$1 [L])

Guide link: https://dev.to/vumanhtrung/setup-an-htaccess-file-for-redirecting-to-laravel-s-public-folder-1e1j

0 likes
5 replies
Snapey's avatar

this is NEVER a good idea and such articles should be marked as dangerous

you risk exposing all your .env secrets

Laravel is not designed to be installed in such a way

1 like
BuenosAires's avatar

@Snapey Thank your for taking your time to answer my doubts, the subfolder is private, is .env still at risk? the only public file would be the .htaccess. If is still dangerous, how would you install Laravel given this conditions then?

public_folder 
public_folder/private_folder //where all the laravel files should go

Laravel project structure is the opposite of this, /public (root) is a subfolder and everyting else is private. If i put everything that is in /public folder in public_folder it breaks as all pathing is relative to laravel default project structure. (example: index.php's require __DIR__.'/../vendor/autoload.php';).

Is there any documentations you could recommend me for deploying on shared hosting? All i found was on cloud hosting deployment which doesnt help me in this situation.

Once again, thank you for answering.

vural2123's avatar

It looks like the issue is that the second .htaccess file in the public folder is also trying to redirect the request, causing the URI to be appended with "subfolder_name/public" multiple times. To fix this, you can add another RewriteCond directive to the .htaccess file in the public folder to check if the request has already been rewritten, and to ignore it if it has. Here's an example of how you can do this:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^(.*)$ index.php [L] </IfModule> This will check if the request has already been rewritten by checking if the REDIRECT_STATUS environment variable is empty, and if it is, it will rewrite the request to point to index.php. If the request has already been rewritten, the condition will be false and the rewrite rule will be ignored.

Alternatively, you can also use the RewriteCond %{REQUEST_URI} !/subfolder_name/public in the .htaccess file in the public folder, to check if the request URI is not already pointing to the public folder.

1 like

Please or to participate in this conversation.