Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Apache HTTP and Nginx Last updated: January 23, 2021
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

I had my site under subfolder and I decided to move to the root level of the domain.

The main challenge was to keep all articles on the same path as a subfolder while redirecting the URL to the root of the domain or another location.


Apache HTTP server supports RewriteCond. To make this change, edit your .htaccess or httpd.conf file and add the below lines to it:

RewriteEngine on
RewriteBase /
RewriteRule ^subfolder/(.*)$ /$1 [R=301,NC,L]

Here we’re using / as our base and our RewriteRule works for all URLs in our subfolder by using regular expressions. R=301 specifies a permanent redirect.


We can achieve similar redirection with the Nginx web server using the rewrite directive in your /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf:

location ^~ /subfolder {
     rewrite ^/subfolder(.*)$ $1 last;

Here ^~ modifier ensures that this prefix location continues to take precedence if you were to add any regex locations in the future.


In IIS, we can use the Rewrite module to rewrite URLs that are in a subfolder and we want to appear as if they were in the root. You can edit your web.config file in your webroot folder (%SystemDrive%\inetpub\wwwroot) with something like:

<?xml version="1.0" encoding="UTF-8"?>
                <rule name="Root_URL_Rewrite" stopProcessing="true">
                    <match url="^(.*)" />
                    <action type="Rewrite" url="/subfolder/{R:0}" />

Alternatively, you can additionally use the URL Rewrite UI in IIS Manager to add an inbound rule.


We can achieve similar redirection using Cloudflare by following below steps:

  • Log into your Cloudflare account.
  • Click the appropriate Cloudflare account for the domain where you want to add URL forwarding.
  • Click the Rules app, then click the Page Rules tab.
  • Under Page Rules, click Create Page Rule. The Create Page Rule dialog opens for your domain.
  • Now, under If the URL matches, enter the URL pattern for a subfolder that should match the rule, like
  • Next, click Add a Setting and choose Forwarding URL from the drop-down menu.
  • Click Select Status Code and choose 301 (Permanent Redirect) or 302 (Temporary Redirect).
  • Enter the destination URL for your root domain and click Save and Deploy to finish.

The above example is to redirect everything from to

Wrapping Up

As you can see, redirecting or mapping a subfolder to root or another URL is a common task and is supported by most popular browsers with different options.

Next, find out how you can host multiple websites on a single IP.

  • Chandan Kumar
    As the founder of Geekflare, I’ve helped millions to excel in the digital realm. Passionate about technology, I’m on a mission to explore the world and amplify growth for professionals and businesses alike.
Thanks to our Sponsors
More great readings on Apache HTTP
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder