How to Redirect But Keep Original URL using .htaccess


rewrite url keep original url

Sometimes, you need to redirect your web pages to a new domain or URLs but keep the original URL in web browser. You can easily do this using .htaccess file in Apache web server. Let us take a look at how to redirect but keep original URL using .htaccess.

 

How to Redirect But Keep Original URL using .htaccess

Here’s how to redirect but keep original URL using .htaccess in Apache web server.

 

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

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

 

After you have enabled .htaccess follow these steps to redirect URL but keep the original URL.

 

1. Open the .htaccess file

Open the .htaccess file in a text editor


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

 

2. Add URL rewrite rules

Let’s say you want your users to be redirected from example.com to example2.com but the web browser URL should still show example.com

In that case, add the following rewrite rules to .htaccess. Replace example.com and example2.com with your domain names


Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^ http://www.example2.com%{REQUEST_URI} [L,NE,P]

 

Save and close the file.

 

3.  Restart Apache 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! Now Apache will automatically redirect all requests to example.com to example2.com but keep original URL in web browser.

 

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!