views:

61

answers:

2

I want to redirect m.example.com to example.com/index.php?type=mobile while inheriting the rules I've already written for example.com...

So, say, I have N rules already defined looking something like:

^view/([A-z]+) index.php?view=$1
^delete/([A-z]+) index.php?delete=$1
^page/view/([A-z]+)/([0-9]+) index.php?view=$1&page=$2
^page/delete/([A-z]+)/([0-9]+) index.php?delete=$1&page=$2

Without having to rewrite each of those for the m.example.com subdomain, is there a way for m.example.com to inherit the same rules, but with the subdomain flag type=mobile? Basically, without having to add N more lines for an .htaccess located in the m subdomain folder:

^view/([A-z]+) index.php?view=$1&type=mobile
^delete/([A-z]+) index.php?delete=$1&type=mobile
^page/view/([A-z]+)/([0-9]+) index.php?view=$1&page=$2&type=mobile
^page/delete/([A-z]+)/([0-9]+) index.php?delete=$1&page=$2&type=mobile
+1  A: 

You could simply check the HTTP_HOST within your PHP script:

$mobile = isset($_SERVER['HTTP_HOST']) && strtolower($_SERVER['HTTP_HOST']) === 'm.example.com';
Gumbo
ok that's a neat trick - does HTTP_HOST point to the right domain/subdomain in all cases, or is it like some $_SERVER variables which might be blank sometimes (HTTP_REFERER, for example)?
ina
@ina: *HTTP\_HOST* is the value from the HTTP header field *Host*. It may be missing if the client uses HTTP 1.0 (that’s the reason for `isset($_SERVER['HTTP_HOST'])`).
Gumbo
if that's the case, any suggestions on backup for clients who use HTTP 1.0?
ina
@ina: No. If the client does not send this information, there is chance to distinguish what host was requested. Except when you have different IP addresses for the standard and mobile variants. Another option would be to inspect the User Agent string.
Gumbo
user agent can easily be faked though.. so the HTTP_HOST is from client, and not from serverside.. I guess figuring out the htaccess mod_rewrite would be the only sure way to catch the other cases?
ina
@ina: No, if the client does not send a *Host* field in the request, *HTTP\_HOST* is not available in mod_rewrite neither. And faking the User Agent string is useless pointless in this case as it would only be used to distinguish the variant of the resource.
Gumbo
A: 

So try something new:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example.com [NC]
RewriteRule ^view/([A-z]+) index.php?view=$1&type=%1

Is that what are you searching for ?

Michal Drozd