How to disable redirection from ‘yes-www’ to ‘no-www’ without affecting other kinds of redirections?

I want my domain name of my WordPress blog to be example.com rather than www.example.com (together with the links in the web pages). But WordPress can automatically redirect example.com to www.example.com via redirect_canonical. Though this function can be disabled entirely by putting remove_filter('template_redirect' 'redirect_canonical') in the theme’s php file, I think there are many other useful redirections in the function so I want to disable the redirection from example.com to www.example.com only. Currently this cannot be done solely via the server’s URL rewrite configuration because it will cause an infinite redirection loop. I also check the source code, finding following possibly relevant snippets:

    // www.example.com vs example.com
    $user_home = @parse_url(home_url());
    if ( !empty($user_home['host']) )
            $redirect['host'] = $user_home['host'];
    if ( empty($user_home['path']) )
            $user_home['path'] = '/';

and

Read More
    // Ignore differences in host capitalization, as this can lead to infinite redirects
    // Only redirect no-www <=> yes-www
    if ( strtolower($original['host']) == strtolower($redirect['host']) ||
            ( strtolower($original['host']) != 'www.' . strtolower($redirect['host'])
              && 'www.' . strtolower($original['host']) != strtolower($redirect['host']) ) )
            $redirect['host'] = $original['host'];

So do I have to change the source code or not? In either case, what should I do? Thank you.

UPDATE: Or more appropriately, how to do this by a custom template_redirect handler?

Related posts

Leave a Reply