Elizabeth_Geniveve
Elizabeth_Geniveve

Reputation: 17

.htaccess redirect, exclude subdomain

I've been researching my question, rewording it in many ways but I still can't seem to find an answer. It could also be that it can't be done, hopefully someone can help me. I'm pretty new to .htaccess files but I've been creating a mobile website for my company's corporate site.

What I want it to do is when you go to the mobile site, it will give you an option to see our full corporate site or the mobile site. I have it redirecting to that page just fine, but when you chose to see the full site, it will redirect you back to the same page, instead of seeing the full site. I have the mobile site in a subdirectory "www.companysite.com/mobile". Can I have it so that when you choose the option to view the full site it won't keep redirecting you to the mobile site? Here is my code right now:

    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]

Thank you in advance!

Upvotes: 0

Views: 465

Answers (2)

Oren102
Oren102

Reputation: 26

Some people like using cookies but it's a little more tricky. A sample solution would be:

    ## 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

Of course you would need to change mobile.website in the code above and use js or some other method to inject a mobilecookie.

Personally, I like the HTTP_REFERRER condition more.

Upvotes: 1

Liviu Gelea
Liviu Gelea

Reputation: 361

I think you can also check the referrer. If the referrer is your own site you don't do the redirection. Something like

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

edit: I should give the complete solution:

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

Upvotes: 0

Related Questions