.htaccess。重定向除子域外

时间:2014-01-10 17:02:16

标签: apache .htaccess mod-rewrite redirect mobile

我有一个名为 example.com 的域名和一个移动版 m.example.com 的子域名 和另一个子域名。 xxx.exmaple.com我不想重定向 当我将 example.com 重定向到 m.example.com 时使用.htaccess,它还会将 xxx.example.com 重定向到 m.example.com

我有以下代码

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
  RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
  RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if mobile=0 is set and set cookie 'mobile' equal to 0
 RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
 RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

 # cookie can't be set and read in the same request so check
  RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
  RewriteRule ^ - [S=1]

 # Check if this looks like a mobile device
 RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
 RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera   
       mobile|palmos|webos|googlebot-mobile" [NC,OR]
   RewriteCond %{HTTP:Profile}       !^$

  # Check if we're not already on the mobile site
 RewriteCond %{HTTP_HOST}          !^m\.
 # Check to make sure we haven't set the cookie before
 RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
# Now redirect to the mobile site
 RewriteRule ^ http://m.example.com%{REQUEST_URI}? [R,L]

1 个答案:

答案 0 :(得分:1)

在下面添加以下条件代替RewriteCond %{HTTP_HOST} !^m\.

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$

你的最后一条规则是:

RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile}       !^$
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.com%{REQUEST_URI}? [R,L]