pixeline
pixeline

Reputation: 17974

.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?

Upvotes: 5

Views: 4464

Answers (1)

szemian
szemian

Reputation: 2801

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.

Upvotes: 10

Related Questions