Sometimes web administrators need to redirect one or more URLs in a subfolder to another subfolder in Apache server. This is commonly required if you are restructuring or moving your website. Although, you can do this on client side using JavaScript, it is better to permanently redirect URLs from server side using .htaccess file in Apache. In this article, we will learn how to redirect subfolder using .htaccess in Apache.
Why Redirect Subfolder
1. Restructuring Site – Quite often, web admins need to restructure their website to make it easy to crawl for search engines, and help users browse content. While doing do, they will need to move one or more subfolders from one location to another. In such cases, you will need to redirect subfolder, to redirect users as well as search engines from old URLs to new URLs.
2. Update Content – You will need to redirect all URLs in a subfolder if its content is outdated and you have created a new subfolder with fresh content.
3. Subdomain to Subfolder – Often web admins redirect subdomain content (e.g. blog.site.com) to its own subfolder (e.g. site.com/blog) to get better SEO authority. In such cases, you need to redirect to subfolder.
4. Handle Deleted Subfolder – If you have completely deleted a subfolder for some reason or the other, then you will need to redirect its URLs to home page or elsewhere to avoid 404 page not found error.
5. Canonicalization – Sometimes, URLs on a website work both with and without trailing slash. This can affect your SEO since search engines may see it as duplicate content. So you may need to redirect URLs with trailing slash to those without, or the other way round.
How to Redirect Subfolder using htaccess
There are a couple of common ways to redirect subfolder using .htaccess file in Apache server. Let us say you want to redirect all URLs from https://mysite.com/product to https://mysite.com/new-product
1. Using Internal Rewrite
In this solution, we redirect URLs from one subfolder to another without changing browser URL in user’s browser. This is useful if you want to simply load content from another subfolder without letting the user know that they are being redirected. In such cases, open or create .htaccess file in your website’s root folder and add the following lines of code in it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/product/(.*)$ new-product/$1 [L]
Let us look at the above lines in detail. The first line checks if mod_rewrite is enabled. Next line checks if the requested URL is for a file. The third line checks if the requested URL is for a directory. The last line rewrites all URLs starting with /product to subfolder starting with /new-product. In it, $1 stands for the request URL string after /product/
2. Using External Redirect
In this case, the browser will change URL to the new redirected URL. In this case, add the following lines to .htaccess file.
RewriteEngine On
RewriteCond %{HTTP_POST} ^(www\.)?mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/product(/.*)?$
RewriteRule ^/product/(.*)$ new-product/$1 [R=301,L]
In the above code, the first line checks if mod_rewrite is enabled. The next line checks if the domain is mysite.com. Next, line checks that the requested URL does not start with /product. The last line rewrites all URLs starting with /product to /new-product.
Variations
Let us look at some of the variations of the above problem. If you need to redirect all pages of a specific subfolder /product to the home page or root page then add the following rewrite rule.
RewriteEngine On
RewriteRule ^product(/.*)?$ https://mysite.com [R=301,L]
You can also do the same thing using Redirect directive.
Redirect 301 "/new-product" "https://mysite.com"
To rewrite all URLs of your site to a specific subfolder, without changing browser URL, add the following lines to your .htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ new-product/$1 [L]
Conclusion
In this article, we have learnt how to redirect subfolder using .htaccess in Apache server. We have described a couple of methods to do this – using internal rewrite and using external redirect. One method allows you to redirect without changing browser URL while the other allows you to redirect by changing browser URL. You can use any of these methods depending on your requirement.
Also read:
How to Configure Apache Cache in Ubuntu
How to Increase File Upload Size in Apache
How to Disable Cache in Apache Server

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.