Redirecting Your Main Site to a Subfolder the Right Way

If you need to redirect http://www.yourdomain.com to http://www.yourdomain.com/subdirectory/ without causing a redirect loop, you must configure your .htaccess file properly. Below is the correct method to achieve this.

1. Add This Code to Your .htaccess File

Place the following rules in the .htaccess file located in your website’s root directory. Make sure to replace yourdomain.com with your actual domain and newfolder with the name of your subdirectory.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteCond %{REQUEST_URI} !^/newfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /newfolder/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule ^(/)?$ newfolder/index.php [L]

2. Understanding the Rewrite Rules

  • RewriteEngine on – Enables mod_rewrite for URL redirection.
  • RewriteCond %{HTTP_HOST} – Checks if the request is coming to your domain.
  • RewriteCond %{REQUEST_URI} !^/newfolder/ – Ensures that already redirected requests are not redirected again, preventing a loop.
  • RewriteCond %{REQUEST_FILENAME} !-f – Ensures that existing files are not affected.
  • RewriteCond %{REQUEST_FILENAME} !-d – Ensures that existing directories are not affected.
  • RewriteRule ^(.*)$ /newfolder/$1 [L] – Redirects all requests to the subfolder.
  • RewriteRule ^(/)?$ newfolder/index.php [L] – Ensures the main site loads newfolder/index.php.

3. Save and Upload the .htaccess File

After adding the code, save your .htaccess file and upload it to your root directory. Then, clear your browser cache and test the redirection.

4. Need Help? @CustomerPanel Is Here for You

If you face issues with your redirects, reach out to UnderHost Support. We provide expert server management, including dedicated servers and cloud hosting, ensuring your website runs smoothly.