.htaccess

@VPHrishikesh said:

  • I found this one, but, I don’t know which all UAs to incude in mobile browser:

RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule ^abc.html$ http://example.com/xy/opera.html [R=301]

Simple enough (although it’s missing the RewriteEngine On statement in case you were wondering why it didn’t work). But you’d have to write and maintain the list of specific user agent filters yourself, which is tricky.

@VPHrishikesh said:

That looks quite interesting actually.

I see two options to use their templates:

  • Use the Apache option and copy it to a .htaccess file on your account, and change the URL on the last line in the file.
  • Use the PHP option and copy the code to the top of your own script. This, of course, only works if you write the software yourself (and you’re more comfortable writing PHP than fiddling with .htaccess rules).

The .htaccess file works essentially the same as you described in option one, but comes with a fairly complete filter set already.

@VPHrishikesh said:

  • Here’s another one that I think I had tried:

RewriteEngine On

# Check if this is the noredirect query string
RewriteCond %{QUERY_STRING} (^|&)noredirect=true(&|$)
# Set a cookie, and skip the next rule
RewriteRule ^ - [CO=mredir:0:%{HTTP_HOST},S]

# Check if this looks like a mobile device
# (You could add another [OR] to the second one and add in what you
# had to check, but I believe most mobile devices should send at
# least one of these headers)
RewriteCond %{HTTP:x-wap-profile} !^$ [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} !\\smredir=0(;|$)
# Now redirect to the mobile site
RewriteRule ^ http://m.example.org%{REQUEST_URI} [R,L]

The nice thing of this code is that it actually allows you to redirect people to your mobile site, but also lets you provide a “View Desktop Site” button by setting a cookie or adding stuff to a URL to prevent people from being redirected again right away.

On the other hand, this code doesn’t do much in terms of device detection. It only checks for the x-wap-profile and Profile headers, which I don’t think any smart phone actually sends.

Yeah. The apache script from that website worked! Thanks for the help!