htmanning has asked for the wisdom of the Perl Monks concerning the following question:

I have a CMS that writes static files. We're trying to provide a stripped version of the site for mobile browsers and we're doing this with a perl script. What I need is a way to strip query string info and pass it to the script. So our rss feed would have a link like this:
www.site.com/article.html?id=1234&site=mysite
I need a way through the .htaccess file to strip everything after the question mark and pass it to something like:
www.site.com/cgi-bin/article.pl?id=1234&site=mysite
So I'm using the following to detect the mobile browsers and pass them to the script, but it doesn't really work. It passes them to the script but includes the entire original URL after the question mark:
RewriteEngine on RewriteCond %{REQUEST_URI} !^/mobile/.*$ RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Ericsson" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Samsung" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "vodafone" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "iPhone" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "nokia" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "symbian" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "Opera Mini" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "BlackBerry" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "j2me" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "midp" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "htc" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "java" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "sony" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "android" [NC,OR] RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC] RewriteRule (.*) http://www.site.com/cgi-bin/article.pl?$1
The last line doesn't really work. Any idea if this is doable? I know it's slightly off topic but there are some smart people around here. Thanks.

Replies are listed 'Best First'.
Re: Grabbing query string
by CountZero (Bishop) on Jan 31, 2011 at 07:03 UTC
    Interesting question, but where is the Perl question?

    Did you have a look at the docs for the Apache rewrite-engine (assuming you are using the Apache webserver)?

    Perhaps the following rewriterule can help you (warning: I have not tested this!):

    RewriteRule www.site.com/article.html?(.*) http://www.site.com/cgi-bin +/article.pl?$1
    or this
    RewriteRule .* http://www.site.com/cgi-bin/article.pl?%{QUERY_STRING}

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Grabbing query string
by chrestomanci (Priest) on Jan 31, 2011 at 09:21 UTC

    Why attempt to identify mobile users? why not let them decide if they want the stripped down mobile site, or the full fat desktop site. I think it would be better if you setup an alternative URL for users who want the stripped down site such as: http://m.<yoursite>.com

    That way users who are using fancy smartphones and tablets with large screens and fast data connections can choose to use the desktop site if they want to, while users on underpowered desktop computers on the end of slow dial up connections can use the mobile site to save on bandwith and site complexity.

    Having said that, if you realy need to identify mobile browsers, then take a look at WURFL. It is a database of thousands of mobile browser user agent strings, with the properties of each device so you can taylor a site for screen size, JavaScript support etc.

    There is a perl module Mobile::Wurfl to interface to WURFL though when I tried using it I found it did not have enough features. The author did not accept a patch I sent, so I ended up writing my own WURFL libary which my employer will not allow me to share.

      Thanks for the suggestions. We have a separate mobile site and I still give people the choice, but this is mainly for Twitter feeds. It's working. I got help from here:
      http://corz.org/serv/tricks/htaccess2.php
      If anyone else needs this I got it to work by doing this in the .htaccess:
      RewriteCond %{QUERY_STRING} ID=(.+) RewriteRule ^articles/(.*) http://www.site.com/cgi-bin/mobile.pl?$1 [Q +SA]
      It only forwards people who use mobile browsers and who click on a link that specifies an ID in the query string. Everyone else is routed normally. Thanks. Thanks for the help.