How to Alias URL to Another in Apache


alias url to another in apache

When you move a web page, if you are unable to redirect the old page to new one, then it is advisable to alias your old URL (e.g /test) with your new one (e.g /prod), that is, when users loads /prod, your website should load /test without changing the URL. This will let search engines and website visitors see the new content. So let us take a look at how to alias URL to another in Apache.

 

How to Alias URL to Another in Apache

Here are the steps to Alias URL to another in Apache.

 

Before proceeding, please ensure you have enabled .htaccess (mod_rewrite) in your Apache web server. Here are the steps to do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html)

 

1. Open .htaccess file

Open .htaccess file using a text editor. It is generally located at /var/www/html.


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

 

2. Alias URL to Another

Let’s say you want to alias a URL /product to /new-product, then simply add the following lines to your .htaccess file.

RewriteEngine On
RewriteRule ^/product$ /new-product [R,L]

The above code will ensure that when users access /product, your website loads content for /new-product without changing the URL in their browsers’ address bars.

 

If you want to alias an entire directory (e.g /solutions/) to a new directory (e.g /new-solutions/)

RewriteEngine On
RewriteRule ^/solutions(/?.*)$ /new-solutions/$1 [R,L]

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

That’s it! You can access your website or specific directories via localhost.

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!