[wp-hackers] Fixed issue with mobile site compatibility

Johnathan rollervegan at gmail.com
Sun Oct 3 19:17:35 UTC 2010


Hi,

I found and fixed an issue in Wordpress: If you have a mobile version of
your site on a different subdomain (ie, m.mydomain.com), all pages for
single posts send a 301 redirect to the www. version of the site, instead of
staying on the mobile site.

For example, you have two aliases for your website:

*www.example.com
m.**example**.com*

Both point to the same Wordpress installation. The "m." version is the
stripped down version for mobile phones... this is the same mobile site
naming convention that is used on Facebook and a number of other popular
websites.

Wordpress was redirecting all single posts (is_single() == true) to the
"www." version of the site when browsing the mobile site.

The problem was in wp_includes/canonical.php. I fixed it by adding the
following 3 lines of code at line 344 (Wordpress 3.0.1). If the requested
URL is on the "m." version of the site and it thinks it wants to redirect to
"www.", replace "www." with "m." so it stays on the same version of the
site.

*// if they came in on the mobile version of the site (eg. m.example.com),
stay on the mobile site*
*if ((strncasecmp( $requested_url, 'http://m.', 9) == 0) && (strncasecmp(
$redirect_url, 'http://www.', 11) == 0))*
*    $redirect_url = "http://m.".substr( $redirect_url, 11);*

It would be great if someone could integrate this change into the Wordpress
codebase, as I don't have that ability myself. I have also attached the
modified version of the file.

Please note that if you're testing this code, Wordpress has a habit of
including the full domain name in all URLs, so you will need to strip off
the domain name in order to make your site work on alternate subdomains. You
can do so by sending your site output through a function such as the
following:

*function CleanURLs( $input)*
*{*
*    // remove our host name from URLs*
*    return str_replace("http://www.**example**.com/", "/", $input);*
*}*

Thanks!

Johnathan


More information about the wp-hackers mailing list