ina
ina

Reputation: 19534

htaccess mod_rewrite subdomains inheriting rules from main domain

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

Upvotes: 0

Views: 618

Answers (2)

Michal Drozd
Michal Drozd

Reputation: 1351

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 ?

Upvotes: 0

Gumbo
Gumbo

Reputation: 655239

You could simply check the HTTP_HOST within your PHP script:

$mobile = isset($_SERVER['HTTP_HOST']) && strtolower($_SERVER['HTTP_HOST']) === 'm.example.com';

Upvotes: 1

Related Questions