Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect root to subfolder

I would like mod_rewrite to redirect all requests to non existing files and folders, and all requests to the main folder ("root") to a subfolder. So i've set it up like this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC,OR]
RewriteCond %{REQUEST_URI} / [NC]
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]

Unfortunately, it does not work: if i request example.com/public/ it redirects to my processing script (so redirecting to my/subfolder/index.php?app=public ) although the folder "public" exists. Note that requesting domain.com/ correctly redirects to my/subfolder/index.php

Why is that?

like image 730
pixeline Avatar asked Feb 27 '23 12:02

pixeline


1 Answers

it does not work because your last condition is not matching only the root but any uri that has / in it, which is basically everything. Try the following instead:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]

Note that [NC] is not needed as you are not trying to match any alphabets, so "No Case" is not really needed.

Hope it helps.

like image 108
szemian Avatar answered Mar 07 '23 22:03

szemian