Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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.
Digging Into WordPress: Take your WordPress skills to the next level.

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

  1. I think this check is not good, because we can have the string “www.” in the domain name (rarely) like http://www.mywww.com. It’s better to check with regular expression or strpos like this:

    if (strpos($_SERVER['HTTP_HOST'], 'www.') === 0) {
    // Redirect
    }

  2. Jeff Starr 2009/02/08 1:48 pm

    Good catch, Tuan. That may be a rare case, but definitely something for people to consider. Thanks for mentioning it! :)

  3. use pregmatch (not begin with) :

    if (!preg_match("/^www./i",$_SERVER['HTTP_HOST'])){
    }

  4. Jeff Starr 2009/02/21 7:29 pm

    Sweet tip, BorisB — I love collecting those sweet little snippets! :)

  5. Hi Jeff! Sorry for being away for so long, I feel bad for not commenting for ages. Thanks for sharing the short but very useful snippet of code to remove the ‘www’ away from web addresses – that saves your site address 3 unnecessary letters, and like what you’ve mentioned, make things more ordered and uniform throughout.

    Oh, and speaking of canonical URLs, I’ve read on WP forums that some people are having problems with comments submission after they’ve switched to them. The crux of the problem lies with comments.php where the form submits the comment (alongside with all the metadata) wrongly to wp-comments-post.php, resulting the stripping away of all the comment information.

    There is some fix around and it occurs only to certain blogs. So far I haven’t tried it out for myself (partially for the fear that something like that will happen), so what are thoughts on this, Jeff?

  6. Jeff Starr 2009/02/25 1:52 pm

    Hi teddY, great to hear from you, as always! I haven’t read anything about the issue with WordPress canonical URLs.. do you know if it’s related to the dashboard settings or the automatically applied PHP redirects for non-canonical URLs? Hopefully the issue is not affecting your site! I use this HTAccess method for all of my canonicalization needs, mostly because I have not updated WordPress since version 2.3-something! ;P

  7. @Jeff You’re welcome! It was just a thread I stumbled upon on the support forum the other day, and a few others responded with the same issue. I guess the problem lies with hard-wiring the form action into comments.php instead of using a PHP expression for it or something? I’m not very sure about that. I’ve never experienced a problem like this before though, no worries!

    Your WP installation is highly customized for sure, and it must be such a big pain in the butt to do upgrades so frequently! Some people complained that WP is actually being updated way too frequently that they fail to catch up – it’s definitely a good thing, since I rarely hear people complaining about fast progress. I can totally understand you as I will also facepalm when I’ve finished modding a plugin and realized that a new version (and sometimes, the new versions are coded groundup) has been released. Dang!

  8. I really really don’t get this…all I want to do is make it to where I don’t have to type “www” in the url bar!!! :P

  9. When I do this I get a “Redirect Loop – Redirection limit for this URL exceeded. Unable to load the requested page.”

    Any ideas why this might be? I’ve tried this on a few domains, alwys get the same outcome. Everything I’ve tried it on has been on a WordPress installation.

    Thanks

  10. Jeff Starr 2009/04/12 8:44 am

    @dave: yeh, it sounds like something is messed up. This is some pretty basic code that shouldn’t be giving you any trouble. Even so, maybe something was lost in translation somehow. If you post the exact code you were using (wrap each line in <code> tags please), perhaps something will jump out as the culprit..

  11. Thanks for the reply

    Here is the exact code I am using in my .htaccess file, in the root of my WP installation:

    # universal www canonicalization via htaccess # remove www prefix for all urls - replace all domain and tld with yours # https://perishablepress.com/universal-www-canonicalization-via-htaccess/ RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^domain\.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

    That is all that is in my .htacess file.

    Same error:

    Redirect Loop. Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked. The browser has stopped trying to retrieve the requested item. The site is redirecting the request in a way that will never complete.

    Any ideas? Thanks

  12. oh, and of course “domain” is replaced with my domain :)

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 »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.