Categories
Apache mod-rewrite and htaccess related Articles htaccess url-rewrewriting

Redirect a domain to another domain without changing URL with htaccess

0
(0)

Last Updated on April 19, 2021 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 0 / 5. Vote count: 0

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

By Amit

I am a freelance web developer/designer , blogger and StackOverflow contributer from India.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code