TransWikia.com

Forwarding between two domains pointed to the same directory of the server without changing the URL

Server Fault Asked by Ghadeer R. Majeed on November 4, 2021

I have domain1.com and domain2.com.

domain1.com is the main domain that is pointed to the server IP 123.123.123.123 exactly to the directory public_html.

domain2.com is the domain that I want to forward to domain1.com without changing the URL in the user’s browser. (Note: this domain will be the domain of the user profile).

So, instead of typing domain1.com/users/John, I will type domain2.com.

domain2.com is pointed to the same IP address 123.123.123.123 and to the same directory of domain1.com (public_html).

I pointed domain2.com to the same server and directory to access it from the htaccess of domain1.com.

So, I tried the following in the .htaccess of public_html:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule ^(.*) https://domain1.sa/users/John/$1 [P]

But this is returning 500 Error http code!

Note: I have a dedicated server.

One Answer

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule ^(.*) https://domain1.sa/users/John/$1 [P]

It's not clear why this would result in a 500 error, unless mod_proxy or mod_rewrite is not installed or configured properly? Or you have a conflict with other directives perhaps? You should check your error log for the specifics of the error.

However, if the two domains point to the same server/directory and you wish to keep domain2.com (the original URL) in the browser's address bar, then you shouldn't have to proxy the request (as you are trying to do here). A simpler internal rewrite (on the same domain) would seem to be all you require. For example:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule !^users /users/John%{REQUEST_URI} [L]

The regex !^users is to avoid a rewrite-loop (which will also manifest itself as a 500 error in the browser).

So, instead of typing domain1.com/users/John, I will type domain2.com.

But note that the above will send the request to /users/John/ (with a trailing slash), not /users/John (no trailing slash) as you've stated here - is that an issue?

And you are wanting to rewrite from /<something> to /users/John/<something> - as per the code sample you posted? Although this is not mentioned in your question text? If you only want to rewrite requests for the domain itself, ie. domain2.com/ and not domain2.com/<something> then the above can be simplified further. For example:

RewriteCond %{HTTP_HOST} ^domain2.com
RewriteRule ^$ /users/John [L]

Answered by MrWhite on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP