How to Do URL Masking in Apache


url masking in apache

Sometimes, when move your website or create new web pages, you might want to mask your URL mainly because it has good domain authority or you don’t want to break site navigation. In this case, you can mask your new URLs such that when users enter old URLs, they see the content of new pages, but their browsers’ address bar will still show the old URL. Let us look at how to do URL masking in Apache.

 

How to Do URL Masking in Apache

 

There are multiple way so do URL masking 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/domain.com)

 

1. Using Virtual Hosts

When a user enters www.domain.com they should be served www.example.com/domain with the browser URL still showing www.domain.com. In that case, add the following lines to your apache config file. We simply setup domain.com to use the base directory of example.com as its DocumentRoot directory.


NameVirtualHost *:80

<VirtualHost *:80>
ServerName example.com
DocumentRoot /www/files

[... all other config you have for example.com]
</VirtualHost>

<VirtualHost *:80>
ServerName domain.com
DocumentRoot /www/files/domain

# To have CSS directly under /www/files/css instead of /www/files/domain/css
Alias /css /www/files/css

[... all other config you have for domain.com]
</VirtualHost>

 

2. Using mod_rewrite

If you want to use mod_rewrite, simply add the following lines to the .htaccess file of domain.com


RewriteRule /(.*) http://example.com/domain/$1 [P]

 

If you want domain.com/css to be example.com/css then add rules to catch the URLs and then redirect them


RewriteRule /css/(.*) http://example.com/css/$1 [P]
RewriteRule /(.*) http://example.com/domain/$1 [P]

 

 

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]

 

 

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!