I have a bunch of domains that might be coming in with or without a www. With some of these domains I want to redirect them if they are mobile to m.domain.com. If I do 1 domain, it works. For example:
RewriteCond %{HTTP_ACCEPT} text/vnd.wap.wml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !(ipad|joojoo|tablet) [NC]
RewriteCond %{HTTP_USER_AGENT} \bagent1|agent2|agent3\b[NC]
RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
RewriteRule .* "http\:\/\/m\.domain1.com/" [R,L]
But when I try to capture the domain name and use it in the rule, it doesn't seem to work, for example:
RewriteCond %{HTTP_ACCEPT} text/vnd.wap.wml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !(ipad|joojoo|tablet) [NC]
RewriteCond %{HTTP_USER_AGENT} \bagent1|agent2|agent3\b[NC]
RewriteCond %{HTTP_HOST} ^(domain1\.com)$ [OR]
RewriteCond %{HTTP_HOST} ^www\.(domain1\.com)$ [OR]
RewriteCond %{HTTP_HOST} ^(domain2\.com)$ [OR]
RewriteCond %{HTTP_HOST} ^www\.(domain2\.com)$
RewriteRule .* "http\:\/\/m\.$1/" [R,L]
$1 is not getting set with the domain name. Any idea what I'm doing wrong?
TIA
There are two things wrong with
RewriteRule .* "http\:\/\/m\.$1/" [R,L]
One, you don't need to escape characters (like /
here) in the substitution URL path because it isn't treated as a pattern but just text. Use of $
and %
back references is allowed but no escaping is required as such.
Second, to capture the host sub-group you need %1
instead of $1
because the sub-group comes from RewriteCond
instead of RewriteRule
. So, you can change your .htaccess
to
RewriteCond %{HTTP_ACCEPT} text/vnd.wap.wml [NC,OR]
RewriteCond %{HTTP_USER_AGENT} !(ipad|joojoo|tablet) [NC]
RewriteCond %{HTTP_USER_AGENT} \b(agent1|agent2|agent3)\b[NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?:domain1|domain2)\.com)$ [NC]
RewriteRule ^ http://m.%1/ [R=301,L]
The %{HTTP_HOST}
condition matches for any domain name that starts with www\\.
but makes it optional (?: ... )?
. Followed by either domain1|domain2
followed by .com
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.