Question

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)

Was it helpful?

Solution 3

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.

OTHER TIPS

Maybe:

RewriteEngine on

RewriteCond %{REMOTE_HOST} ^subdomain.* [NC]
RewriteRule ^(.*)$ ^subdomain/ [L]

Replace "subdomain" on your subdomain

Ok try these rules:

RewriteEngine on

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.
RewriteRule ^ /%1%{REQUEST_URI} [L,NC]

make sure this is your first rule.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top