Save 25% on Wizard’s SQL for WP w/ code: WIZARDSQL
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

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:

header('HTTP/1.1 301 Moved Permanently');
header('Location: http://'.substr($_SERVER['HTTP_HOST'],4).$_SERVER['REQUEST_URI']);

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.
Digging Into WordPress: Take your WordPress skills to the next level.

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

  1. Avatar photo
    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. Avatar photo

    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 /.
    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. Avatar photo
    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. Avatar photo

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

    “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 but you have a line in your .htaccess that redirects people to, 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:

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

    thanks for your help.

  5. Avatar photo
    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. 2010/03/10 4:02 pm

    great article, thanks for sharing.

Comments are closed for this post. Something to add? Let me know.
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 »
Digging Into WordPress: Take your WordPress skills to the next level.
Daylight savings is a complete waste of time and needs to be eliminated.
Got a couple of snow days here in mid-March. Fortunately it's not sticking.
I handle all email in real time as it comes in, perpetually clear inbox for years now.
Added some nice features to Wutsearch search engine launchpad. Now 21 engines!
.wp TLD plz :)
Nice collection of free SEO APIs and user-agent lookups for Googlebot, Bingbot, Applebot, YandexBot, and more.
90% of online customer support is just explaining how to do basic troubleshooting.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.