Search code examples
regexapache.htaccessmod-rewritemod-proxy

Redirect to other domain but keep typed domain


After looking on the internet for about an hour, I didn't find the answer to my question. So I'm searching with the wrong keywords or what I want is not possible.

What I want:
I have multiple domains with different extensions, for example:

  • mydomain.be
  • mydomain.nl

Now what I want is that the mydomain.be is redirected to mydomain.nl. The solution for this I have found on the internet and shown below, with the need of .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.be$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.be$
RewriteRule (.*)$ http://www.mydomain.nl/$1 [R=301,L]

With this code, when you type mydomain.be you will be redirect to mydomain.nl. But also the URL in the addressbar is changed to mydomain.nl. What I want is to keep the URL in the addressbar mydomain.be.

So, mydomain.be:

  • keep URL
  • show content of mydomain.nl

How To?


Solution

  • It is possible to get it done via mod_rewrite but make sure mod_proxy is enabled in your Apache's httpd.conf. Once that is done enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.be$ [NC]
    RewriteRule ^ http://www.mydomain.nl%{REQUEST_URI} [L,NE,P]
    

    Take note of flag P which is used for handling the proxy request.

    Read more about flag: P in mod_rewrite