How to Redirect Subdirectory to Subdomain using .htaccess

redirect subdirectory to subdomain

When you restructure your website, sometimes you might end up moving many web pages from your subdirectory (e.g to a subdomain (e.g In that case, you need to redirect those web pages from subdirectory to subdomain so that search engines and website visitors are directed to the right destination. Let us look at how to redirect subdirectory to subdomain using .htaccess


How to Redirect Subdirectory to Subdomain using .htaccess

Here’s how to redirect subdirectory to subdomain using .htaccess. Basically, you need to open .htaccess in a text editor, add redirect rules (given below) in it and restart Apache to apply the changes.

Before we proceed, please ensure you have enabled mod_rewrite for your Apache server. Here’s how you can do it on:

  1. Ubuntu/Debian systems
  2. CentOS and similar systems

.htaccess is a simple file that allows you to set up redirects and URL rewrites without accessing Apache’s server config files. You can find it in your website’s root folder (e.g /var/www/html in Linux). If you can’t find it, you can create it. Just name it .htaccess. Nothing before or after.


1. Edit .htaccess file

Open .htaccess file in a text editor.

$sudo nano /var/www/html/.htaccess


2. Add redirect rules

Let’s say you want to redirect a single URL in subdirectory to subdomain. Replace blog with your subdirectory, and with your domain name

From –
To –

# Permanent URL redirect
Redirect 301 /blog/new-page.html


If you only want to redirect the subdirectory page to subdomain

From –{page-url}
To –{page-url}

# 301 redirect for main pages
RedirectMatch 301 ^/blog/$


In case you want to redirect all URLs in a subdirectory to subdomain

From –
To –

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^
RewriteRule ^blog/(.*)$$1 [L,R=301]


3. Restart Apache Server

Restart Apache server to apply the changes

$ sudo systemctl restart apache2 [Ubuntu/Debian]
$ sudo systemctl restart httpd [RHEL/CentOS]


Congratulations! Apache will automatically redirect subdirectory to subdomain.



About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!