Remove the WWW Prefix for all URLs via PHP
Posted on February 8, 2009 in Function by Jeff Starr
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
# http://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!
Related articles
- Canonical URLs and Subdomains with Plesk
- Universal www-Canonicalization via htaccess
- htaccess Combo Pack: WordPress Permalinks and non-www Redirect
- Unobtrusive JavaScript: 5 Ways to Remove Unwanted Focus Outlines
- Comprehensive URL Canonicalization via htaccess for WordPress-Powered Sites
- Valid, SEO-Friendly Post Translation Links
- Pure CSS: Remove Link Properties for Linked Images with Borders
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}Good catch, Tuan. That may be a rare case, but definitely something for people to consider. Thanks for mentioning it! :)
use pregmatch (not begin with) :
if (!preg_match("/^www./i",$_SERVER['HTTP_HOST'])){}Sweet tip, BorisB — I love collecting those sweet little snippets! :)
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?
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
@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!
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
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
@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..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# http://perishablepress.com/press/2008/04/30/universal-www-canonicalization-via-htaccess/RewriteEngine OnRewriteBase /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
oh, and of course “domain” is replaced with my domain :)
Hi dave, two different things to try:
1. Try removing the
RewriteBaseand see if that works.2. If that fails, try replacing the code with the following:
# universal www canonicalization via htaccessRewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]And of course you will want to change the
domain.tldto match your own. Let me know how it goes!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, addRewriteBase /.Example:
RewriteEngine onRewriteBase /”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?
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
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 tohttp://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.
Ah, that explains it then — thanks for the follow-up post, dave, glad to hear you got it working. :)
great article, thanks for sharing.