Nick
Nick

Reputation: 14283

htaccess - issue when trying to redirecting to mobile website (on subdomain)

I am having some issue with the .htaccess file on one of my sites.

The error I get is that I'm trying to redirecting too many times.

The way it currently works is that I need to redirect the URL to the subfolder /dist everytime I'm on desktop, and if I'm on mobile, I need to go to the mobile version of the website instead which is a subdomain starting with mobile.

my htaccess file looks like this:

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)

# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.mywebsite.com/dist]

RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/www\.mywebsite\.com\/dist" [R=301,L]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST}          !^m\.

# Can not read and write cookie in the same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$)

# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE}        !^.*mredir=0.*$ [NC]

# Now redirect to the mobile site
RewriteRule ^ http://mobile.mywebsite.com [R,L]

can anybody help?

this is my first attempt at something like this

thanks

Upvotes: 1

Views: 181

Answers (1)

anubhava
anubhava

Reputation: 785156

You can have it this way:

RewriteEngine on
RewriteBase /

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)    
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:www.mywebsite.com/dist]

RewriteCond %{HTTP_HOST} ^(?:www\.)?mywebsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ http://www.mywebsite.com/dist [R=301,L]

# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^(m|mobile)\. [NC]    
# Can not read and write cookie in the same request, must duplicate condition
RewriteCond %{QUERY_STRING} !(^|&)m=0(&|$) [NC]    
# Check to make sure we haven't set the cookie before
RewriteCond %{HTTP_COOKIE} !^.*mredir=0 [NC]
# check mobile agents
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android" [NC] 
# Now redirect to the mobile site
RewriteRule ^ http://mobile.mywebsite.com [R=302,NE,L]

Upvotes: 1

Related Questions