I’ve been googling the hell out of the problem I’m having and so far I have found nothing that works.
What I have is a Drupal multi-site installation that I’m trying to setup redirects for mobile on. To start I have 10 domains that have only one common string, www. What I want is to check if the device viewing the site is mobile (I’m using AMF to successfully do this) and make sure that the visitor is not already on the mobile version which will be m.domain.tld. If all is good then I need to redirect the user to the mobile subdomain.
Doing this per domain works using the following:
RewriteCond %{ENV:AMF_DEVICE_IS_MOBILE} ^true$
RewriteCond %{ENV:AMF_DEVICE_IS_TABLET} !^true$
RewriteCond %{HTTP_HOST} !^[m.]+mysite.com$
RewriteRule (.*) http://m.mysite.com/$1 [R=301,L]
I do not, however, want to have 10 of those blocks (1 for each domain) nor do I want to have to create another block every time we create a new website.
I have read that you can use () in the RewriteCond patterns to make a var out of the matching pattern. Unfortunately I don’t think I’m making my pattern properly or something because when I try the following it redirects to m. followed by whatever the request uri is.
I’m leaving out the AMF conditions for brevity.
RewriteCond %{HTTP_HOST} !^[m.]+([^.]\.[com|net|org])$
RewriteRule (.*) http://m.%1/$1 [R=301,L]
From what I read, the %1 should match my pattern from the RewriteCond but it isn’t matching anything. That makes me think my pattern is wrong. I’ve tried changing ([^.].[com|net|org]) to (.*) but get the same issue where it just redirects to m..
Am I right in assuming my pattern is wrong and if so what should I be using? Is what I’m trying to do even possible?
To be clear, my domains are in the form of:
somedomain.org
http://www.maybeanotherdomain.com
In my examples I have the flag R=301 but in testing I’m leaving that out and have to clear a lot of data from the browser each time I test to make sure I’m picking up the new rule.
Thanks in advance for any help!
You can’t backreference a not match (!), but you can separate the condition into 2 matches against the %{HTTP_HOST} variable:
You may need to tailor the
([^\.]+)\.bit to suit your needs, this expression will only match the “domain” part of “www.domain.com”.