Latest TweetsVerify any search engine or visitor via CLI Forward-Reverse Lookup perishablepress.com/cli-forwar…
Perishable Press

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 = Designer. Developer. Producer. Writer. Editor. Etc.
Archives
18 responses
  1. Jeff Starr

    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

    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

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

  6. shor7.com March 10, 2010 @ 4:02 pm

    great article, thanks for sharing.

[ Comments are closed for this post ]