Sometimes you may need to redirect a URL to a different subdirectory on your website. This is a common requirement among websites & blogs as they move their content across folders. You can easily do this in many ways in Apache server. In this article, we will look at the different ways to rewrite URL to subdirectory in Apache server.
How to Rewrite URL to Subdirectory in Apache
Here are the steps to rewrite URL to subdirectory in Apache.
1. Enable mod_rewrite
We will be using mod_rewrite for our purpose. If it is not already enabled in your Apache server, use the following steps to enable it, depending on your Linux distribution.
Ubuntu/Debian
Open terminal and run the following command to enable mod_rewrite.
$ sudo a2enmod rewrite
Redhat/Fedora/CentOS
Open Apache configuration file in a text editor.
$ sudo vi /etc/apache2/httpd.conf OR $ sudo vi /etc/httpd/httpd.conf
Look for the following line.
#LoadModule rewrite_module modules/mod_rewrite.so
Uncomment it by removing # at its beginning. If you don’t find this line, add it afresh.
Also look for the following Directory tag and change AllowOverride from None to All.
. . .
<Directory /var/www/html>
. . .
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
. . .
</Directory>
. . .
2. Rewrite URL to Subdirectory
Open .htaccess file at your website’s root folder
$ sudo vi /var/www/html/.htaccess
Apache allows you to redirect URLs using RewriteRule, RedirectMatch and RewriteCond directives. Let us say you want to redirect URL /product.html to /data/product.html. Here is how you can accomplish it using the different directives available in Apache server.
Using RewriteRule
Add the following lines to your .htaccess file.
RewriteEngine On RewriteRule ^/product\.html$ /data/product\.html [L,R=301]
If you want to redirect all URLs in folder /product to another subfolder /data then you can use the following RewriteRule instead.
RewriteEngine On RewriteRule ^/product/(.*)$ /data/$1 [R=301,L]
Using RedirectMatch
You can alternatively use RedirectMatch directive to redirect URL /product.html to /data/product.html.
RewriteEngine On RedirectMatch ^/product\.html$ /data/product\.html
Using Redirect
Similarly, you can also use Redirect directive to redirect URL /product.html to /data/product.html.
RewriteEngine On Redirect ^/product\.html$ /data/product\.html
If you want to redirect all URLs in folder /product to another subfolder /data then you can use the following Redirect directive instead.
RewriteEngine On Redirect /product /data
If you want to redirect URLs to another folder in another domain, just replace the destination folder in each of the above commands to include the new domain name, that is, https://www.newdomain.com/data instead of /data. Here is an example of Redirect command to redirect URLs to another folder on a different domain.
Redirect /product https://www.newdomain.com/data
3. Restart Apache Server
Restart Apache server to apply changes.
$ sudo service apache2 restart
In this article, we have seen different ways to redirect URLs from one folder to another. You can modify them as per your requirement.
Also read:
How to Rewrite URL Parameters in Apache
How to Combine Two Files in Linux
How to Rewrite URL to Another URL in Apache
How to Filter Logs by Date/Time in Linux
How to Verify Checksum in Linux