4.3
(16)

Last Updated on January 8, 2022 by Amit

This article explains how to set a subfolder as root folder for your domain name using RewriteRule directive in an htaccess file.

With an htaccess file , you can easily manage your site URLs the way you want to. You can shorten a long URL, you can completely change the appearance of your site URLs and you can do even more.
If you are new to htaccess and URL rewriting , I have written a Short htaccess tutorial for beginners that you might find interesting and helpful.

This post will show you how to set your subdirectory as main directory using a few lines of htaccess code.
Assuming your folder structure is as follows :

  • /root
  • /subfolder 1
  • /subfolder 2
  • index.php
  • .htaccess

As you can see we have two subfolders in the root directory , to access files or folders inside subfolder1 we have to type https://example.com/subfolder1/file and to access any files/folders in root we just type https://example.com/file .

Okay ,
if you want to set your subfolder1 as your main directory, by setting “subfolder1” as main directory here I mean to say your subfolder will be treated as your root folder. Any requests you receive on your domain via root folder will be mapped to the subfolder1.

The following basic htaccess rule rewrites your root folder and any root requests to /subfolder

RewriteEngine On
#rewrite root to subfolder
#including all files and folders
#exclude the destination folder
RewriteCond %{REQUEST_URI} !^/subfolder1 [NC]
RewriteRule ^(.*)$ /subfolder1/$1 [L]

Okay, isn’t it as simple as ABC?

The rule does the following :

It redirects any files and folders from root to subfolder.
The redirection is internal so your browser won’t redirect and your visitors will not be able to notice it.
The URL example.com/file.php will show you the contents of example.com/subfolder1/file.php
.

Some important things to take note of in the rule :

  • We excluded the destination folder using RewriteCond directive as it’s important to avoid rewrite loop error.
  • We used ^(.*)$ instead of ^(.+)$ in the pattern above so that any empty URI (Root folder “/”) can also map to the subfolder1.
  • Use the rule in your root htaccess only and remove your subfolder1 htaccess if it exists.

Okay, the rule above does its job perfactly but there is still something important to implement. You now have two URLs pointing to the same location, ie . example.com/file.php and example.com/subfolder1/file.php both URLs will show you the same content which can cause duplicate content issues on search engines.
If you care about your site SEO, you will need to fix it. You can set up a 301 redirect from your subfolder1 URLs to the root so that search engines can index only one URL.

The following is the complete version of the htaccess rules to map root to subfolder

RewriteEngine On
#301 Redirect subfolder1 to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subfolder/(.*)$ /$1 [L,R=301]
#rewrite root to subfolder
#including all files and folders
#exclude the destination folder
RewriteCond %{REQUEST_URI} !^/subfolder1 [NC]
RewriteRule ^(.*)$ /subfolder1/$1 [L]

I hope this article was helpful. If you still need any help regarding root to subfolder redirection , just leave a comment below .I would love to help you out with this. Thanks.

How useful was this post?

Click on a star to rate it!

Average rating 4.3 / 5. Vote count: 16

No votes so far! Be the first to rate this post.