John John - 1 month ago 13
Apache Configuration Question

rewrite subdomain using .htaccess

My main domain is

subdomain.domain.tld
, and I want to rewrite all the traffic from
subdomain1.domain.tld
to the first one. Meaning if someone accesses
subdomain1.domain.tld/whatever.php
, he actually accesses
subdomain.domain.tld/whatever.php
, however, he's still shown
subdomain1
in the browser's navigation bar.

I did some research, but I couldn't find something too promising.

Answer

You don't need rewriting for this, in fact internal rewriting is not possible between separate hosts...

Assuming that both "subdomains" (those are actually hostnames) are served by the same http server you can simply configure the same DocumentRoot for both hosts. That way they serve exactly the same file system which obviously means that the same scripts will be called.

Maybe you can get away even more simple if you just use the ServerAlias command for your virtual host. This obviously is only possible if you do not need separate configurations for both hosts.

Just take a look into the documentation of the apache http server. This is explained and good examples are offered: https://httpd.apache.org/docs/2.4/vhosts/examples.html


In case those two hosts are not served by the same http server you could use an internal proxy setup: subdomain1.domain.tld acts as a front end proxy for subdomain.domain.tld, so it just relays all incoming requests and also the outgoing responses. That is easily done with a combination of the ProxyPass and the ProxyPassReverse rules offered by apaches proxy module: https://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypassreverse

This setup can even be used if the two http servers operate on different IP addresses or even completely separate systems.