Search code examples
php.htaccessurl-maskingdomain-maskingdomain-mask

Grabbing masked URL in php from htaccess


How can I grab the masked URL in PHP from the htaccess? I have already tried HTTP_HOST, REQUEST_URI and SERVER_NAME but it always returns .com when I am trying to grab the masked url being .nl in this instance. HTTP_REFERER is not reliable and doesn't always have anything to refer from.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteRule ^(.*) http://domain.com/$1 [P] 

Solution

  • PHP always sees the host under which it was called, which in your case alsways will be domain.com. However, a solution would be to include a query parameter in the rewritten URL, like:

    RewriteRule ^(.*) http://domain.com/$1**?from=domain.nl** [P]
    

    Depending on whether you may have a query-string in the incoming URL, you possibly will need two RewriteCond-RewriteRule Combinations, one starting the query-string with a question mark ? and one appending to it with an ampersand & if the question mark is already there.