How to redirect the request from default main domain folder to sub folder

Author: | Posted in Domain, Learn, Server No comments

Apache server provide a very useful and handy mode-rewrite module, .htaccess. This file can be used to overwrite the server settings and redirect all requests from main domain to the target sub folder.

For example, consider a domain ‘www.domainname.com’ which you want to redirect to a sub folder ‘subfolder’.

RewriteEngine on
 RewriteCond %{HTTP_HOST} ^(www.)?domainname.com$ [NC]
 RewriteCond %{REQUEST_URI} !^/subfoldername/
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /subfolder/$1
 RewriteCond %{HTTP_HOST} ^(www.)?domainname.com$ [NC]
 RewriteRule ^(/)?$ subfolder/index.php [L]

Line 1 ensures that server know that you are going to use mod-rewrite.
Line 2 checks if the request is domainname.com or www.domainname.com and proceeds to the next line.
Line 3 checks if the request destination is not the subfolder. Then go to next line.
Line 4 checks if the requested name is not an existed file in public_html.
Line 5 checks if the requested name is not an existed directory in public_html.
Line 6 redirects request to subfolder.
Line 7 checks if the request is domainname.com or www.domainname.com and proceeds to the next line.
Line 8 redirects request to the default file(index.php) under subfolder.

Add Your Comment