redirect to subfolder htaccess

How To Redirect To Subfolder Using .htaccess

Sometimes you may need to redirect to subfolder when you move your website’s root folder contents to a new subdirectory. You can easily redirect to subdirectory using .htaccess file in Apache web server. Here’s how to redirect to subfolder using .htaccess file.

 

How To Redirect To Subfolder Using .htaccess

Here’s how to redirect all traffic to a subfolder using .htaccess file. Please ensure that you have enabled mod_rewrite in your Apache server before you follow these steps. Only then your .htaccess configuration will be applied by your server. After you redirect to subdirectory, you may want to use a charting tool to monitor the key metrics about your website/application such as signups, purchases, etc. using dashboards & charts to ensure that everything is working as before, and spot issues early.

 

1. Open .htaccess file

You will typically find .htaccess file in your site’s root folder (e.g /var/www/html/). You can open it using vi editor

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

 

2. Redirect to Subfolder

There are multiple ways to redirect to subfolder (e.g /product). We will look at each of them. You can use any of them as per your requirement.

Using RewriteRule

Add the following lines to your .htaccess file.

RewriteEngine On
RewriteRule ^$ /product [L]

The above lines will redirect all requests sent to the root, to the /product subfolder.

 

Similarly, you can also redirect HTTP to HTTPS using .htaccess.

Using RedirectMatch

If you only want to redirect root and not other URLs to a subfolder, you can use RedirectMatch

RedirectMatch ^/$ /product/

 

Using RewriteCond

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ product [L]

The above rules work for both www.example.com as well as just example.com. Similarly, you can redirect from subfolder to subdomain in Apache.

 

3. Restart Apache Server

Restart Apache Server to apply changes

$ sudo service apache2 restart

 

That’s it! Now your website will automatically redirect all requests to example.com/product subfolder.

By the way, if you want to create charts & dashboards to monitor your business or website, you can try Ubiq. We offer a 14-day free trial!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!