Using .htaccess to redirect obsolete browsers - .htaccess

Im trying to redirect few browsers using htaccess to a page that would suggest to update their browser and I cant figure it out with the .htaccess file:
Currently I am using:
RewriteCond %{HTTP_USER_AGENT} "MSIE 6"
RewriteCond %{REQUEST_URI} !^/ie/
but that transfers only internet explorer 6. How can I add more browsers and where can I find their user_agent names? I found this article online: http://evansims.com/6032/using-htaccess-to-redirect-obsolete-browsers/ which suggests this:
RewriteCond %{HTTP_USER_AGENT} "Opera/3" [or]
RewriteCond %{HTTP_USER_AGENT} "Opera/2" [or]
RewriteCond %{HTTP_USER_AGENT} "Opera/1" [or]
RewriteCond %{HTTP_USER_AGENT} "Safari/4" [or]
RewriteCond %{HTTP_USER_AGENT} "Safari/3" [or]
RewriteCond %{HTTP_USER_AGENT} "Safari/2" [or]
RewriteCond %{HTTP_USER_AGENT} "Safari/1" [or]
RewriteCond %{HTTP_USER_AGENT} "Navigator/" [or]
RewriteCond %{HTTP_USER_AGENT} "MSIE 6" [or]
RewriteCond %{HTTP_USER_AGENT} "MSIE 5" [or]
RewriteCond %{HTTP_USER_AGENT} "MSIE 4" [or]
RewriteCond %{HTTP_USER_AGENT} "MSIE 3" [or]
RewriteCond %{HTTP_USER_AGENT} "MSIE 2" [or]
RewriteCond %{HTTP_USER_AGENT} "MSIE 1"
RewriteRule . upgrade.html [L]
but it doesn't seem to work for me. Is there any website that provides a list with the http user agents in bulk? For example when I say Firefox 3.0 it means all versions including 3.6
This website: http://www.useragentstring.com/pages/Firefox/ has hundreds of user agents which Im pretty sure there is a way to include all of them in one row? ( Like IE6,7 Firefox 1 to 3 (including 3.6), Opera 1 to 10 etc )

# Jeff
You can't use [1-15] because that says 1 thru 1 or 5
You need to use something like [1-9] which means 1 thru 9. You can can't use double digits in a range like that.
So you would have to do it like so:
RewriteCond %{HTTP_USER_AGENT} Firefox/[1-9][0-5]?\. [NC,OR]
[1-9][0-5]?
Means 1 thru 9, with or without a 0 thru 5

RewriteCond %{HTTP_USER_AGENT} Opera/[1-3] [OR]
RewriteCond %{HTTP_USER_AGENT} Safari/[1-4] [OR]
RewriteCond %{HTTP_USER_AGENT} Navigator/ [OR]
RewriteCond %{HTTP_USER_AGENT} MSIE [1-6]
RewriteRule .? upgrade.html [L]
This should work better.

See this post: Mobile or desktop browser detection and redirect to respective web page
Can you do it with php or other backend instead? The following is for mobile detection, but you could easily put in your own user agent list. If the condition is met
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
if(preg_match('/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i',$useragent)||preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i',substr($useragent,0,4)))
// insert php redirect code here
?>

Related

htaccess blocking access based on length of user agent string

I recently came across several cloud based services image scraping my website and had to block them based on their User Agent string.
However this would be very inefficient if I had to check my logs on a regular basis to find out who they are and add them to the htaccess block list.
After investigations it transpired that these bots all have user agent strings less than about 12 characters e.g. "moe".
So I was looking for a way to block user agents based on the length of the UA string. But of course you cannot perform calculations in htaccess.
So I came up with the following:
RewriteCond %{HTTP_REFERER} ^$
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{12}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{11}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{10}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{9}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{8}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{7}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{6}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{5}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{4}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{3}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{2}$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^[a-z0-9./:;-=]{1}$ [NC]
RewriteRule .*\.(js|ico|gif|jpg|png|swf|rm|mov|mpg|mpeg)$ xxxxx/hotlink.gif [F,NC,L]
I would appreciate any feedback on this if I have missed any likely characters that could be used by UA or if anyone can see any problems with this approach.
I do have all the other usual image hotlinking in place.
This is to prevent cloud services hotlinking to images from newsletters.
Thanks in advance.

Exclude Images from Mobile Redirect on htaccess

I am using htaccess to create a mobile redirect for my website. Basically it changes mysite.com to m.mysite.com. In the process it also changes the url of images. How can I prevent that? Below is the code I am using.
# turn on rewrite engine
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/
# only detect smart phone devices if we are not on mobile site
# to prevent redirect looping
RewriteCond %{HTTP_HOST} !^m.easydestination.net$
# a bunch of smart phone devices
RewriteCond %{HTTP_USER_AGENT} "sony|symbian|nokia|samsung|mobile" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "windows ce|epoc|opera|mini|nitro" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "j2me|midp-|cldc-|netfront|mot" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up\.browser|up\.link|audiovox" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "mini|nitro|j2me|midp-|cldc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "netfront|mot|up\.browser|up\.link"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "audiovox|blackberry|ericsson,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "panasonic|philips|sanyo|sharp|sie-"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "portalmmm|blazer|avantgo|dange"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|series60|palmsource|pocketpc"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "smartphone|rover|ipaq|au-mic,"[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "alcatel|ericy|vodafone\/|wap1\."[NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wap2\.|iPhone|android"[NC,OR]
# redirect google mobile bot
RewriteCond %{HTTP_USER_AGENT} "googlebot-mobile"[NC]
# if the request is from any one of the above devices
# redirect to mobile site
RewriteRule .? http://m.easydestination.net%{REQUEST_URI} [L,R=302]
You can add this, line before the last RewriteRule:
RewriteCond %{REQUEST_URI} !\.(?:gif|jpe?g|png)$ [NC]
To exclude all files with .gif .jpeg .jpg .png
Do not forget after to use absolute links for those images.

Mobile Redirect Issue

So i have been trying to create a mobile redirect for a portion (service area) of my site. The way the site is built requires me to use the htaccess file to accomplish this. I have the code in and for some reason any visitors coming from Google AdWords or Map Listings are forwarded to the mobile site, whether or not they are on mobile/desktop. This is the only case i have an issue.
My thought was to create a redirect back to the standard site from the mobile site for non-mobile visitors. Will this create a loop or will this actually work? Also how would the code work to designate, if NOT mobile, redirect to specific page. Here is what i have:
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]
RewriteRule ^mobile-homepage$ http://www.example.com/homepage [L,R=302]
You want to get rid of all the OR flags in your conditions. You don't want "Not A" or "Not B" (because then only B would make conditions true), you want "Not A" AND "NOt B"
RewriteCond %{HTTP_ACCEPT} !text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml [NC]
RewriteCond %{HTTP_USER_AGENT} !sony|symbian|nokia|samsung|mobile|windows ce|epoc|opera [NC]
RewriteCond %{HTTP_USER_AGENT} !mini|nitro|j2me|midp-|cldc-|netfront|mot|up\.browser|up\.link|audiovox [NC]
RewriteCond %{HTTP_USER_AGENT} !blackberry|ericsson,|panasonic|philips|sanyo|sharp|sie- [NC]
RewriteCond %{HTTP_USER_AGENT} !portalmmm|blazer|avantgo|danger|palm|series60|palmsource|pocketpc [NC]
RewriteCond %{HTTP_USER_AGENT} !smartphone|rover|ipaq|au-mic,|alcatel|ericy|vodafone\/|wap1\.|wap2\.|iPhone|android [NC]
RewriteRule ^mobile-homepage$ http://www.example.com/homepage [L,R=302]
The only questionable bit is the first condition. There's not way to logically group that OR everything else.

Link from mobile site after htaccess redirect to desktop version

I have a mobile version of a site http://www.m.fdl.de/
and a desktop version http://www.fdl.de/
With a .htaccess redirect I link all mobile users to the mobile version, what is running nice.
On my mobile Site I set a link in the footer that should link back to the desktop version.
I tried it with several .htaccess solutions I found on stackoverflow but with no success.
my link is always linking me back to the mobile site.
This is my .htaccess . I found it on this post: Mobile Redirect using htaccess
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.fdl.de]
# 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} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC]
# Check if we're not already on the mobile site
RewriteCond %{HTTP_HOST} !^m\.
# Can not read and write cookie in 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://m.fdl.de [R,L]
It woul be very nice if someon coul help me!
I might be really late, but i found out that the link to the full site must be like this:
Full Site
Hope it helped.

htaccess stop external script requests by domain match?

we've got a few sites using the same cms and we keep getting hacked which we're looking at now.
Problem is its taking a while and its really hard to find all the compromised database entries quickly.
In the meantime how can we stop requests to external servers with htaccess?
ie if there's a request to an external domains script that contains ".ru" block?
any help would be much appreciated!!!
best, Dan.
ok found this article which has methods to do this;
http://stopmalvertising.com/security/securing-your-website-with-htaccess/Page-4.html
even five a list of other notorious robots to block;
##############################################################
# stop the hacks!
# useragents starting with
RewriteCond %{HTTP_USER_AGENT} ^atraxbot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Azureus [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^geohasher [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^PycURL [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Python-urllib [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^research-scan-bot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Sosospider [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^xenu [NC,OR]
# user agent contains string
RewriteCond %{HTTP_USER_AGENT} ^.*onmult.ru [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*casper [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*goblox [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*sun4u [NC]
RewriteRule ^(.*)$ - [F]
best, Dan.

Resources