Plugin Sale! Save 15% on pro plugins with discount code: FALL2020
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 = Web Developer. Book Author. Secretly Important.
WP Themes In Depth: Build and sell awesome WordPress themes.

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 »
The Tao of WordPress: Become your own WordPress guru.
Thoughts
Watching. Waiting. Praying.
Got all of my free WordPress plugins updated for imminent WP 5.6 in early December. Pro plugin updates currently in the works.
7G Firewall now integrated into BBQ Firewall (free version). Pro version soon ;)
macOS Big Sur update complete. So far no crazy issues. Except TextEdit, which is completely screwed up and unusable. Replaced with free BBEdit.
Got so sick of macOS’ annoying “red dot” that I had to remove System Prefs from the dock. Come on Apple you can do better.
Beginning development of an Nginx version of 7G Firewall.
Happy Birthday to Perishable Press, celebrating 15 years online! :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.