Spring Sale! Save 30% on all books w/ code: PLANET24
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!

About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

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

  1. 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 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 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 »
.htaccess made easy: Improve site performance and security.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.