Gabriel Meono
Gabriel Meono

Reputation: 1010

Include subfolder with .htaccess

I have a sub folder with php files.

Like this: domain.com/subfolder/file.php

It would be very useful to call the php files as if they were on the root folder, without ignoring the existing root files.

Is there a way to include a subfolder and all its contents through .htaccess?

Upvotes: 2

Views: 1948

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following rule in root/.htaccess :

 RewriteEngine on


 #1--If the request is not for an existent root directory--#
RewriteCond %{REQUEST_FILENAME} !-d
 #2--And the request is not for an existent root file--#
RewriteCond %{REQUEST_FILENAME} !-d
#3--Then, rewrite the request to "/subfolder"--#
RewriteRule ^([^/]+)/?$ /subfolder/$1 [NC,L]

The RewriteConditions above are important to avoid rewriting your root folder and files to /subfolder.

Or try this :

RewriteEngine on
#--if /document_root/subfolder/foo is an existent dir--#
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -d [OR]
#--OR /document_root/subfolder/foo is an existent file--#
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1 -f
#--rewrite "/foo" to "/subfolder/foo--#
RewriteRule ^(.+)$ /subfolder/$1 [NC,L]

Upvotes: 2

Rinat
Rinat

Reputation: 429

How about something like this?

RewriteRule ^subfolder/(.*)$ $1

It should help I think

P.S please make sure that apache rewrite module is enabled and turned of in .htaccess file

Upvotes: 0

Related Questions