How to Redirect Subdirectory to Another Subdirectory using .htaccess


redirect subfolder to another subfolder

When you restructure your website or move around certain pages, you need to redirect old URLs to their new locations so that users and search engines know that your pages have moved. Here’s how to redirect subdirectory to another subdirectory using .htaccess.

 

How to Redirect Subdirectory to Another Subdirectory using .htaccess

Here are the steps to redirect subdirectory to another subdirectory using .htaccess

Before you proceed, please ensure that you have enabled .htaccess (mod_rewrite) in Apache. If not, then here’s how you can do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html) and add the following rules, depending on your use case.

 

Redirect subdirectory to another subdirectory in same domain

If you want to redirect only one URL to another


RewriteRule ^old-url-string$ http://www.example.com/new-url-string [R=301,L]

 

OR

 


Redirect 301 /path-to/old-url http://www.example.com/path/new-url

 

If you want to redirect all URLs in subdirectory to another


RewriteRule ^subdirectory/(.*)$ /newdirectory/$1 [R=301,NC,L]

 

If you want to redirect all URLs in a subdirectory to the root URL


RewriteRule ^subdirectory/(.*)$ /$1 [R=301,NC,L]

 

Redirect subdirectory to another subdirectory in another domain

To redirect all URLs in a subdirectory to the same subdirectory in another domain


Redirect 301 /subdirectory http://www.newdomain.com/subdirectory

 

If you want to redirect only one URL to new URL on another domain


RewriteRule ^old-url-string$ http://www.newdomain.com/new-url-string [R=301,L]

 

OR

 


Redirect 301 /path-to/old-url http://www.newdomain.com/path/new-url

 

If you want to redirect all URLs in subdirectory to another subdirectory in another domain


RewriteRule ^subdirectory/(.*)$ http://www.newdomain.com/newdirectory/$1 [R=301,NC,L]

 

If you want to redirect all URLs in a subdirectory to the root URL of another domain


RewriteRule ^subdirectory/(.*)$ http://www.newdomain.com/$1 [R=301,NC,L]

 

Force HTTPS over a subdirectory

If you want to redirect an entire subdirectory to go over HTTPS, then add the following lines to .htaccess


RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} subdirectory
RewriteRule ^(.*)$ https://www.example.com/subdirectory/$1 [R,L]

 

 

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!