Redirecting to AMP (reader mode) for mobile

I’m not a big fan of AMP but I do have it active here on this blog using the official AMP plugin for WordPress, using it in “Reader” (aka “classic”) mode. That’s as far as I want to take it, but suppose (as was the case for an Autoptimize/ Critical CSS user I was helping out) you want to redirect all mobile traffic to AMP, then you could use below code snippet to do just that.

add_action('init','amp_if_mobile');
function amp_if_mobile() {
  $_request = $_SERVER['REQUEST_URI'];
	if ( strpos( $_request, '?amp' ) === false && strpos( $_request, '&amp' ) === false && ! is_admin() && wp_is_mobile() ) {
    if ( strpos( $_request, '?' ) === false ) {
      $_amp = '?amp';
    } else {
      $_amp = '&amp';
    }
    $amp_url = home_url() . $_request . $_amp;
    wp_redirect( $amp_url );
    exit;
  }
}

It might be a little rough around the edges, but it (mostly) gets the job done 😉

MSIE 6 drops anchor in Location-header, must die!

http://ripie6.com/Last week colleagues of mine had a problem with an e-mail newsletter they wanted to send out; everything worked OK in Firefox and IE 7, but MSIE 6 displayed the wrong part of the page.
The setup was pretty basic; the URL in the newsletter pointed to the servers of the mailinglist-provider, where each request got logged and the browser was redirected (with a http 302 status-code and Location in the http response-header) to the target URL on one of our servers. That target URL contained an anchor to have the browser to display a specific tab on the page thanks to some jQuery-magic, which worked perfectly in Internet Explorer 6 in a non-redirect scenario.
The problem seemed as simple as it was annoying; MSIE 6 dropped everything starting with the ‘#’ from the URL when performing a redirect. Google pointed me to some sites that claimed that adding an ampersand should solve this, but that did not work. I made a little PHP-script to test with different encoding-tricks, but that did not work. So that old fart of a browser indeed did not support anchors in redirect-URL’s and that’s what I told the colleagues last Thursday.
Yesterday I started writing this post, thinking it was a great time to demand the death of the piece of junk that Microsoft unleashed on us back in 2001 and which, believe it or not, still has 16,94% market share. So  I replaced the company-specific address in the php-script with the URL of the wikipedia-page about anchors and … it just worked, even in MSIE 6! And then I remembered getting that silly popup in MSIE 6, warning me that “The current website is trying to open a site in your Trusted sites list”. Apparently the ugly bugger does not only ask you if he can redirect, but also eats the anchor in target-URL’s that are in your Trusted sites.
So dear colleagues, in case you’re reading this; you can send out that newsletter now, it’ll work for everyone except for those who are silly enough to use MSIE 6 with our site in their “Trusted sites”-list. And let’s not forget; MSIE 6 must die is dead!