.htaccess重定向,排除子域名

时间:2012-04-03 13:41:01

标签: .htaccess mod-rewrite mobile redirect

我一直在研究我的问题,在很多方面重新措辞,但我似乎无法找到答案。它也可能是无法完成的,希望有人可以帮助我。我对.htaccess文件很新,但我一直在为我公司的公司网站创建一个移动网站。

我想要它做的是当您访问移动网站时,它会为您提供查看我们完整的公司网站或移动网站的选项。我将它重定向到该页面就好了,但是当您选择查看完整网站时,它会将您重定向回同一页面,而不是查看完整网站。我将移动网站放在子目录“www.companysite.com/mobile”中。我可以拥有它,以便当您选择查看完整网站的选项时,它不会将您重定向到移动网站吗?这是我现在的代码:

    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} android
    RewriteRule .* http://companysite.com/mobile [R=301,L]

    RewriteCond %{HTTP_USER_AGENT} BlackBerry
    RewriteRule .* http://companysite.com/mobile [R=301,L]

提前谢谢!

2 个答案:

答案 0 :(得分:1)

有些人喜欢使用cookies,但这有点棘手。一个示例解决方案是:

    ## Setting a cookie and variable for mobile users
    RewriteRule .* - [E=IsMobile:false]
    RewriteRule .*  - [E=MobileCookie:false]

    # Mobile Redirection
    # Blackberry
    #RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} "blackberry" [NC]
    RewriteRule ^$ http://mobile.website/ [L,R=302]
    # End Blackberry

    # Mobile Redirection for IPhone
    RewriteCond %{HTTP_USER_AGENT} "googlebot-mobile|iemobile|iphone|ipod|opera mobile" [NC]
    RewriteRule ^$ http://mobile.website/ [L,R=302]
    # End other IPhone

    # Mobile Redirection for other Mobile
    RewriteCond %{HTTP_USER_AGENT} "android|palmos|webos" [NC]
    RewriteRule ^$ http://mobile.website/ [L,R=302]

    # Set isMobile variable to true
    RewriteRule .* - [E=IsMobile:true]
    # End other Mobile

    ## Assume the mobile cookie is not set:
    RewriteRule .* - [E=MobileCookie:false]

    ## Checking for mobile cookie
    RewriteCond %{HTTP_COOKIE} gpf_mobileoff=true
    RewriteRule .* - [E=MobileCookie:true]

    ## Combining IsMobile & MobileCookie to make one rule
    RewriteRule .* - [E=ShowMobile:false]
    RewriteCond %{ENV:IsMobile} ^true$
    RewriteCond %{ENV:MobileCookie} !^true$
    RewriteRule .* - [E=ShowMobile:true]
    ########## End - Custom redirects

当然,您需要在上面的代码中更改mobile.website并使用js或其他方法注入mobilecookie。

就个人而言,我更喜欢HTTP_REFERRER条件。

答案 1 :(得分:0)

我认为您也可以查看推荐人。如果引荐来源是您自己的站点,则不进行重定向。像

这样的东西
RewriteCond %{HTTP_USER_AGENT} android
RewriteCond %{HTTP_REFERER} !yoursite\.com
RewriteRule .* http://companysite.com/mobile [R=301,L]

编辑: 我应该给出完整的解决方案:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (android|BlackBerry)
RewriteCond %{HTTP_REFERER} !yoursite\.com
RewriteRule .* http://companysite.com/mobile [R=301,L]
相关问题