Last Updated on January 18, 2022 by Amit

Redirect domain without changing URL
In this post I will show you how to write a rule to redirect your domain to an external domain without changing the URL in browser address bar .

Redirect domain without changing the URL .
You need to enable proxy module on your apache web server in order to map/redirect (internally) your domain to an another domain that is being hosted on a diffrent web server.
once the proxy module is enabled , you can then use mod-rewrite [P] flag which is mainly used to proxy requests between two diffrent HTTP_HOST headers .

Using P flag to rediect domain URLs

P flag is used to rewrite domain URLs between two diffrent host headers .
Assume you have a domain named www.example.com and you want to redirect all its URLs to a diffrent domain named example2.com without changing the browser URLs or preserving the domain name, you can use the following RULE in your htaccess file for this kind of internal redirection.

RewriteEngine On

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

or the following domain specific rule :

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule (.*) https://example2.com/$1 [P]

Your webserver might return a 500 internal server error for using these rules with P flag if you have not enabled the proxy module. If you have already enabled the module then any request for www.example.com domain will be served from the example2.com domain.

How useful was this post?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 13

No votes so far! Be the first to rate this post.