Search code examples
http-redirectmaskingsubdomain

htaccess mask url with subdomain


I want to mask a url path with a subdomain. Example: 'auto.domain.com' should redirect to 'domain.com/auto'

and after that if a menu is pressed the subdomain should remain Example: 'auto.domain.com/busses' and not to go to 'domain.com/auto/busses'

I have managed the redirect with the following:

RewriteCond %{HTTP_HOST} !www\. [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com  [NC]
RewriteRule .*  http://domain.com/%1     [L,R=302]

But this just do the redirect not the masking.

Both domain.com and subdomain.com point to the same DOCUMENT_ROOT. When you go to one or another without any rewrite the domain or subdomain remains in the links. The issue is that subdomain should not go to domain.com but domain.com/auto. (auto is not a folder, its an alias from cms)


Solution

  • Since htaccess is not my strong point, i did a php header redirect:

    $domain = $_SERVER["SERVER_NAME"];
    $requri = $_SERVER['REQUEST_URI'];
    
    if (($domain == "subdomain.domain.com") && $requri == "/" ||
    ($domain == "www.subdomain.domain.com")) { 
    header("Status: 301 Moved Permanently");
    header("Location: http://domain.com/subdomain"); 
    }
    

    This solution is working like it's supposed to, except from some file submit issues that need some additional tweaks for the subdomains.