In case you hate the ‘www.’ part in your website’s URL (like I started doing about two months ago) you should let others — including search engines — know about that. This is simply a matter of redirecting every page accessed through a www. URI to its non-www. equivalent.
Here are two ways to accomplish this: through PHP, or through the use of .htaccess mod_rewrite rules.
The PHP method
<?php
if ('mathiasbynens.be' !== $_SERVER['HTTP_HOST']) {
header('Location: https://mathiasbynens.be' . $_SERVER['REQUEST_URI'], null, 301);
}
?>
This script sends headers that redirect the browser to the non-www address and tells search engines like Google that the move is permanent so they can update their indexes.
The downside of using this method is that this code has to be included in every page you want it to work in. To me, this isn’t a problem though, as I’m working with a header.php file, which I include in every page of this site.
The mod_rewrite method
An alternative is to put the following in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^mathiasbynens\.be [NC]
RewriteRule ^(.*)$ https://mathiasbynens.be%{REQUEST_URI} [R=301,L]
Obviously, the mod_rewrite method is far easier to implement than the PHP script. However, some servers provide limited .htaccess functionality — and in that case, you can still use PHP.
Comments
Eric wrote on :
Mathias wrote on :
Rick Yribe wrote on :
Albert wrote on :
Simon wrote on :
Popola wrote on :
Mathias wrote on :
AllanUp wrote on :
Jeff wrote on :
James wrote on :
Mathias wrote on :