skip to Main Content

I`ve been tasked to modify the redirect to a mobile site in .htaccess file to redirect only the main page. The problem is that the main folder includes many subfolders that are pages of their own and they wrongly redirect to the main site mobile version.

This is the code:

 RewriteOptions inherit
<IfModule mod_expires.c>
ExpiresActive On
#ExpiresDefault "access plus 10 second"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
# Webfonts
ExpiresByType application/vnd.ms-fontobject         "access plus 1 year"
ExpiresByType font/eot                              "access plus 1 year"
ExpiresByType font/opentype                         "access plus 1 year"
ExpiresByType application/x-font-ttf                "access plus 1 year"
ExpiresByType application/font-woff                 "access plus 1 year"
ExpiresByType application/x-font-woff               "access plus 1 year"
ExpiresByType font/woff                             "access plus 1 year"
ExpiresByType application/font-woff2                "access plus 1 year"
</IfModule>

<filesMatch ".(js)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

#### MOBILE REDIRECTION START

RewriteBase /

# Check if m=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/(?: Ballot169)?
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

# Check if m=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/(?: Ballot169)?
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/(?: Ballot169)?
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule ^ - [S=1]

# Check if this looks like a mobile device

# Check if we're not already on the mobile site
# Check to make sure we haven't set the cookie before
# Now redirect to the mobile site
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}       !^$
RewriteCond %{HTTP_HOST}          !^m.
RewriteCond %{HTTP:Cookie}        !mobile=0(;|$)
RewriteCond %{REQUEST_URI} !^/.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/(?: Ballot169)?
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule ^ http://m.siteurl.com [R,L]

#### MOBILE REDIRECTION END

Can someone help me where and how to put the exceptions for folders(sites) other than the main one.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the answer. I will clarify a little bit more. I created a new site on the hosting that has a number of other (mucher older) sites. As I tried to enter the site from my mobile phone I realized that it is redirected to the mobile site of that "main site". I took a look at public_html folder and realized that someone created a site a long time ago in public_html folder with a redirect in .htaccess and then someone else created a number of subfolders with other sites, including a subfolder for the site that I created and uploaded to their server. The problem now is that those sites in the subfolders get redirected to the mobile site of the site that is created in the main public_html folder.

    To answer the "aside" part - It is not my site and I believe that it doesnt. It is a very old site that needs a complete rehaul. Rewrite options inherit - thats my question too. Im not sure, as Im not the creator. It`s complete mess, but for now I just need to resolve the redirect problem.

    I hope that clarifies it a bit.

    EDIT: It seems that removing Rewrite options inherit did the trick. Only the main site gets redirected now to the mobile version.


  2. RewriteRule ^ http://m.siteurl.com [R,L]
    

    The very last RewriteRule directive redirects everything (that matches the regex ^) to the root of the mobile site (although you are strictly missing the trailing slash on the target URL, which the browser will need to append for you.)

    To "redirect only the main page" you need to be more specific with the regex and match an empty URL-path only. eg. ^$. For example:

    RewriteRule ^$ http://m.siteurl.com/ [R,L]
    

    However, the user could presumably enter the site at any page, so do you not want to redirect any URL to the corresponding URL-path path on the mobile site?

    For example, to redirect /<anything> to http://m.siteurl.com/<anything> you need to capture the URL-path using the RewriteRule pattern. For example:

    RewriteRule (.*) http://m.siteurl.com/$1 [R,L]
    

    Where $1 is a backreference to the captured URL-path (ie. (.*)).


    How to write an exception for a specific folder

    The title of your question seems to conflict with what you are asking in the main body of the question, ie. redirecting only the main page.

    However, to answer this… to make an "exception for a specific folder", you would add an additional RewriteCond directive preceding the above RewriteRule directive to check that the request does not map to the "specific folder".

    For example:

    RewriteCond %{REQUEST_URI} !^/specific-folder/
    RewriteRule (.*) http://m.siteurl.com/$1 [R,L]
    

    Only if the requested URL does not (! prefix) start with /specific-folder/ will the following RewriteRule substitution occur.


    Aside:

    • Is your mobile site not using HTTPS?

    • RewriteOptions inherit – What directives are you "inheriting"?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search