Hosting Guide

How to Point Your Main Domain to a Subfolder Without a Redirect Loop (.htaccess)

Redirecting a domain to a subfolder requires precise .htaccess rules to prevent infinite loops. Learn how to configure your server correctly and ensure smooth redirection without breaking your website.

Short answer: Decide first whether visitors should see the subfolder in the address bar. To keep the address as example.com while serving files from /newfolder/, use an internal rewrite in .htaccess with a RewriteCond %{REQUEST_URI} !^/newfolder/ line, which stops the loop. To send visitors to example.com/newfolder/, use a 301 redirect that applies only to the home page. Both versions are below, ready to copy.

Rewrite or redirect: which do you need?

Internal rewrite 301 redirect
Address bar shows example.com/page example.com/newfolder/page
Visitor sees a change No Yes, the URL changes
Good for Keeping a site in a subfolder for tidiness, with clean URLs Moving the site to a new path for good
SEO No change to URLs Search engines move rankings to the new URLs over time

Most people who ask for a “redirect to a subfolder” actually want the internal rewrite: the site lives in a folder, but visitors should not notice.

Why redirect loops happen

A rule such as “send everything to /newfolder/” also matches requests that already point to /newfolder/. Apache applies the rule again, adds the folder again, and so on, until the browser stops with ERR_TOO_MANY_REDIRECTS or the server returns a 500 error. The fix is a condition that skips requests already inside the folder.

Option 1: serve the site from a subfolder (address stays the same)

Open public_html/.htaccess in cPanel’s File Manager, or create it, and add these lines at the top. Replace example\.com with your domain (keep the backslash before the dot) and newfolder with your folder name.

RewriteEngine On

# Serve every request from /newfolder/, unless it already points there
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/newfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /newfolder/$1 [L]

# Serve the home page from the folder too
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(/)?$ newfolder/index.php [L]
Line What it does
RewriteCond %{HTTP_HOST} Applies the rule only to your domain, not to addon domains in other folders.
RewriteCond %{REQUEST_URI} !^/newfolder/ Skips requests that are already in the folder. This is what prevents the loop.
!-f and !-d Leaves real files and folders in public_html alone, for example verification files.
RewriteRule ^(.*)$ /newfolder/$1 [L] Serves the request from the folder, without changing the address.
RewriteRule ^(/)?$ newfolder/index.php Serves the home page, which the !-d check would otherwise skip.

If your site’s start file is index.html, change index.php in the last line.

Option 2: send visitors to the subfolder with a 301 redirect

This changes the address in the browser. Redirect only the home page, so deeper links keep working:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ /newfolder/ [R=301,L]

Because the rule matches only the empty path (the home page), it cannot match /newfolder/ again, so there is no loop. Test with a 302 (temporary) redirect first, then change it to 301, because browsers remember 301 redirects.

WordPress in a subfolder

If the subfolder holds WordPress and you use Option 1, WordPress still builds links with the folder name unless you tell it otherwise:

  1. In Settings > General, keep WordPress Address (URL) as https://example.com/newfolder.
  2. Set Site Address (URL) to https://example.com.
  3. Save, then go to Settings > Permalinks and click Save to refresh the rewrite rules.

WordPress’s own documentation calls this “giving WordPress its own directory”. Take a backup before you change these settings, because a wrong value can lock you out of the admin area.

Why not use cPanel’s Redirects page?

The Redirects page in cPanel is good for sending a whole domain or a page to another address. A wildcard redirect from a domain to a folder on the same domain, however, matches its own target and creates a loop. For a subfolder on the same domain, use the .htaccess rules above.

Test the result

curl -sI https://example.com/ | head -n 5
curl -sI https://example.com/about/ | head -n 5

With Option 1 you should get 200 OK and no Location header. With Option 2, the home page should return 301 with Location: /newfolder/. Clear your browser cache, or use a private window, after each change.

Troubleshooting

Problem Likely cause Fix
ERR_TOO_MANY_REDIRECTS The rule matches its own target, or another redirect (cPanel, plugin or HTTPS rule) conflicts Check the REQUEST_URI condition, and remove duplicate redirects in cPanel and plugins.
500 Internal Server Error A typing mistake in .htaccess Check the error log in cPanel and fix the line it names.
Images and CSS do not load The site uses absolute paths with the folder name, or the folder is wrong Check the paths in the page source, and the folder name in the rules.
Addon domains break The rule applies to every domain Keep the HTTP_HOST condition so the rule only matches your main domain.

For HTTPS and www redirects in the same file, see our free SSL guide. To edit .htaccess safely, see our File Manager guide.

Need help with this?

Need help with redirects or moving a site into a new folder? Our support team can check your rules.

See web hosting plansContact @CustomerPanel

Frequently asked questions

How do I point my domain to a subfolder without changing the URL?

Use an internal rewrite in .htaccess with a condition that skips requests already in the folder, as shown in Option 1.

Why does my redirect to a subfolder loop?

The rule also matches the folder itself. Add RewriteCond %{REQUEST_URI} !^/newfolder/ before the rule, or redirect only the home page.

Should I use a 301 or a 302 redirect?

Test with a 302 first. Switch to a 301 once it works, because browsers and search engines remember 301 redirects.

Can I change the document root of my main domain instead?

On shared hosting the main domain’s folder is fixed to public_html, so the .htaccess method is the usual way. On a VPS with WHM, the administrator can change it.

Does this work with LiteSpeed?

Yes. LiteSpeed reads Apache .htaccess rewrite rules in the same way.


Sources