How to Redirect Without Changing URL with .htaccess


redirect without changing url

Typically, when you redirect users to a different page, the URL displayed in web browser also changes. Sometimes, you need to redirect web pages without changing the underlying URLs. Here’s how to redirect without changing URL with .htaccess.

 

How to Redirect Without Changing URL with .htaccess

Here are the steps to redirect without changing URL, using .htaccess.

Before we proceed, please ensure that you have enabled .htaccess in Apache. If not, then here are the steps to do itL

  1. On Ubuntu
  2. On CentOS

Basically, you add the following rewrite rules in .htaccess file and restart Apache server to apply changes.

 

There are 2 ways to redirect your web pages to a new URL, without changing its domain

Redirect and Retain Everything After the URL

In this case, when you go to your domain’s page, say, domainA.com/your-page it will show you the results on your new domain, domainB.com/your-page

Paste the following lines of code in your .htaccess file and restart Apache to apply changes


RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]

 

Redirect Domain to a specific page

Sometimes, you may want to display content of another website on your website, without fully hosting your website’s content there. For example, you may have a blog on blogspot.com or shopping cart on Etsy but don’t want to fully host your website there. You just want visitors to visit your website but view content of your other websites. In that case, use the following code. Replace the parts in bold with your actual values.


RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/PathToPageHere [P]

 

If you want to change the URL displayed, on redirect, use the Redirect directive. The Rewrite directive will display the redirected page without changing browser’s URL.

 

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!