Search code examples
.htaccessmasking

How can I mask a domain using .htaccess?


We have the following situation:

We would like to setup a domain masking to provide content from a project platform to an end user. The end user has setup a CNAME record from player.domain-client.com. to app.domainA.com

Now when the end user enters https://player.domain-client.com/5432 he should get the contents of https://app.domainA.com/player/?=5432. But the URL should remain https://player.domain-client.com/5432. This masking should only by applied if the client subdomain contains player.

Could anybody point me to the right direction on how to setup the .htaccess so it does the correct masking?


Solution

  • The end user has setup a CNAME record from player.domain-client.com. to app.domainA.com

    Presumably the "project platform" has also been configured to accept requests to player.domain-client.com?

    In which case, it should just be a matter of a simple internal rewrite (on the same host). Although, if you would ordinarily request the same URL-path at app.domainA.com , ie. app.domainA.com/5432, then there is nothing you need to do as the rewrite is already in place? Otherwise, try the following:

    RewriteEngine On
    
    # Rewrite any request for /<number> to player/?=<number>
    RewriteCond %{HTTP_HOST} ^player\. [NC]
    RewriteRule ^(\d+)$ player/?=$1 [L]
    

    However, /player/?=5432 isn't the actual endpoint as this requires further rewriting by the system for it to "work". Perhaps you mean something like /player/index.php?=5432? (The query string is also a little weird as you are missing a parameter name? As written, this would possibly require manual parsing of the query string to extract the value?)

    The condition (RewriteCond directive) ensures that only requests to the player subdomain are rewritten.

    On WordPress you need to make sure these directives go before the WP front-controller. ie. Before the # BEGIN WordPress section. The order of directives in .htaccess is important.

    However, if this is all being managed by WordPress then you can't simply create a rewrite in .htaccess since WordPress still sees the original URL that was requested, not the rewritten URL. So, unless the requested URL exists as a valid route in WordPress itself then you'll likely get a 404. This sort of rewrite needs to be managed inside WordPress itself.

    Alternative solution using a reverse proxy

    An alternative is to configure your server as a reverse proxy and proxy the request from https://player.domain-client.com/1234 to https://app.domainA.com/player/?vid=1234 (mentioned in comments). Ideally this requires access to the main server config to config properly (requires mod_proxy and ProxyPass, ProxyPassReverse directives set appropriate in the virtual host).

    Then, in .htaccess you would do something like the following instead, making use of the P flag on the RewriteRule:

    # Proxy any request for /<number> to player/?=<number>
    # for the "player" subdomain only.
    RewriteCond %{HTTP_HOST} ^player\. [NC]
    RewriteRule ^(\d+)$ https://app.domainA.com/player/?vid=$1 [P]