Welcome to the new design! Please report any bugs or issues, thanks :)
Web Dev + WordPress + Security

Remove the WWW Prefix for all URLs via PHP

Canonical URLs are important for maintaining consistent linkage, reducing duplicate content issues, and increasing the overall integrity of your site. In addition to cleaning up trailing slashes and removing extraneous index.php and index.html strings, removing the www subdirectory prefix is an excellent way to shorten links and deliver consistent, canonical URLs.

Of course, an optimal way of removing (or adding) the www prefix is accomplished via HTAccess canonicalization:

# universal www canonicalization via htaccess
# remove www prefix for all urls - replace all domain and tld with yours
# https://perishablepress.com/press/2008/04/30/universal-www-canonicalization-via-htaccess/

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain\.tld$       [NC]
RewriteRule ^(.*)$       http://domain.tld/$1 [R=301,L]

Unfortunately, not everyone has access to their HTAccess files or Apache configuration file. Fortunately, the same comprehensive (i.e., site-wide) URL canonicalization is easily achieved via PHP:

<?php 
if(!strstr($_SERVER['HTTP_HOST'],'www.')) 
return;
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://'.substr($_SERVER['HTTP_HOST'],4).$_SERVER['REQUEST_URI']);
exit(); 
?>

Paste that code at the top of your PHP-formatted web pages and you’re good to go. For example, if you are using WordPress, you would place the PHP canonicalization script into your active theme’s header.php file. You could also copy the script into your theme’s custom functions.php file, or even upload it to your plugins directory as run it as a plugin. Regardless of which implementation method you use, once the script is in place, you can kiss the www goodbye!

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
Digging Into WordPress: Take your WordPress skills to the next level.

18 responses to “Remove the WWW Prefix for all URLs via PHP”

  1. Jeff Starr
    Jeff Starr 2009/04/14 1:34 pm

    Hi dave, two different things to try:

    1. Try removing the RewriteBase and see if that works.

    2. If that fails, try replacing the code with the following:

    # universal www canonicalization via htaccess
    RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
    RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]

    And of course you will want to change the domain.tld to match your own. Let me know how it goes!

  2. thanks :)

    the first option didn’t work, probably because my server requires that line – from their manual:

    “In the .htaccess file containing your rewrite rules, after RewriteEngine on, add RewriteBase /.
    Example:
    RewriteEngine on
    RewriteBase /

    the second option prevents the redirect loop, but doesn’t remove the “www”.

    perhaps this is an issue in my server configuration? or becuase i have a different domain name being directed to this one using a 301 redirect – perhaps that affects this?

  3. Jeff Starr
    Jeff Starr 2009/04/14 2:02 pm

    Yeh, something is up, but it’s difficult to tell exactly what the issue might be without looking under the hood. Perhaps you can speak with your hosting provider and see if they have any ideas? ..It’s always something, I’ll tell ya’ :P

  4. i managed to get it working. it could be that the problem is with wordpress. see:

    http://markjaquith.wordpress.com/2007/09/25/wordpress-23-canonical-urls/

    “if you have enabled your own form of canonical URL redirection that isn’t redirecting the the URLs that WordPress thinks are the canonical version. For instance, if your blog is http://www.example.com/blog/ but you have a line in your .htaccess that redirects people to http://example.com/blog/, you’re not going to be able to access your site, as the two redirects will ‘fight’ each other in an infinite loop until the browser gives up”

    that appears to be what was happening with my redirect loop.

    i installed this plugin:

    http://txfx.net/files/wordpress/disable-canonical-redirects.phps

    then tried with your .htaccess code, and it works.

    thanks for your help.

  5. Jeff Starr
    Jeff Starr 2009/04/19 3:31 pm

    Ah, that explains it then — thanks for the follow-up post, dave, glad to hear you got it working. :)

  6. shor7.com 2010/03/10 4:02 pm

    great article, thanks for sharing.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Blackhole Pro: Trap bad bots in a virtual black hole.
Thoughts
Super useful post about making custom avatars in WordPress.
Making good progress on the new bookstore subdomain here at Perishable Press. Aiming for launch early Summer :)
Playing the long game.
They have weaponized the idiots.
Good software never steals focus from the user. Even during startup.
After 10 years running my own business, I still manage schedules and tasks using old school post-it notes, sometimes simple sometimes very elaborate.
You know those sites, where you're trying to just grab a quick bit of information but the page is shifting all over the place as it loads up 3 million advertisements.