Last updated on August 10th, 2020 at 03:05 am
When you move a web page to a new location, you need to redirect its old URL to new one. Here’s how to redirect URL to another URL in Apache using .htaccess. You can use it to redirect one URL to another or redirect one page to another.
How to Redirect URL to Another URL
Here are the steps to redirect URL to another URL. There are 2 ways to redirect old page to another page in Apache – using server configuration file and using .htaccess file (mod_rewrite). We will look at both these approaches.
Using Apache Server Configuration
In this case, we redirect one URL to another by modifying Apache server configuration file.
1. Open Apache Server Configuration File
Apache server configuration is located at one of the following locations, depending on your installation setup and Linux version.
- /etc/apache2/httpd. conf
- /etc/apache2/apache2. conf
- /etc/httpd/httpd. conf
- /etc/httpd/conf/httpd. conf
Open Terminal and run the following command to open Apache Server configuration file.
$ sudo vi /etc/apache2/httpd. conf
Bonus Read : How to Redirect All Pages to New Domain in Apache
2. Redirect URL to Another URL in Apache
Let’s say you want to redirect single URL in Apache, that is, redirect /product.html to /new-product.html. Add the following Redirect directive to the VirtualHost tags to your server configuration file.
<VirtualHost *:80> ServerName www.example.com Redirect 301 /product.html /new-product.html </VirtualHost>
In the above code, we use Redirect directive to redirect old URL to new one. Here’s the syntax of Redirect
Redirect [status] source-url destination-url
- status – This is optional. Use 301 for permanent redirect, 302 for temporary redirect.
- source-url – relative URL path you want to redirect
- destination-url – relative or absolute URL path where you want the source URL to be redirected to.
Bonus Read : How to Redirect Site to Another Domain in Apache
3. Restart Apache Server
Restart Apache Server to apply changes.
Ubuntu/Debian:
$ sudo service apache2 start
CentOS/Fedora/Redhat:
$ service httpd restart
Bonus Read : Top 10 SSL Certificate Providers for Your Website
Using .htaccess/mod_rewrite
If you don’t have access to Apache server configuration, or don’t want to change Apache server configuration, then you can redirect one URL to another in .htaccess.
Before you proceed, please ensure you have enabled mod_rewrite in your Apache server. Here are the steps to do it and setup .htaccess file for your website.
1. Open .htaccess file
Open .htaccess file for your website (e.g example.com) in a text editor
$ sudo vi /var/www/example.com/.htaccess
Bonus Read : How to Redirect Domain Without Changing URL
2. Redirect One Page to Another in Apache
Add the following lines to redirect your old page to another page
RewriteEngine on Redirect 301 /product.html /new-product.html
3. Restart Apache Server
Restart Apache Server to apply changes.
Ubuntu/Debian:
$ sudo service apache2 start
CentOS/Fedora/Redhat:
$ service httpd restart
That’s it! Now Apache will automatically redirect URL to another URL, that is, /product.html to /new-product.html.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.