.htaccess redirects for SEO

There has always been one redirect that is Google mandated for SEO. That is to standardize the use of www vs non-www. Allowing use of both on a website can appear as two different urls with the same content. In the long run, this really isn't very important but does tend to be the first step for SEO work. To avoid that penalty, one can use a redirect to make the visitor use one preferred url. 

So what is the preferred code? Believe it or not, I got new instructions on this. Bear with me for a sec here.

That can be easily managed by this being added to the htaccess file (though look at the end of this article for an updated version):

#Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

But wait – stop right there. If you are on a cPanel server and have access to the cPanel account, I recommend you use the Redirects facility offered in the Domains section.

I knew this existed but have never used it much before since I prefer to use the code I've used before. Just didn't trust it. Then when I did, found out cPanel was sticking extra stuff into the htaccess file. Not clean and very worrisome. Well, guess what? Turns out it can be very necessary. cPanel simply has not announced this but this is what is happening.

Every redirect now has these two lines stuck in it:

RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$

And there is actually a reason for this that can be very important on your website.

cPanel now has two different new ways of adding a secure certificate to a website. One is called AutoSSL, generating a freebie certificate – if, and only if, it is turned on in cPanel and the host is running a recent version of cPanel.  The other is done thru Market, cPanel's entry into the secure certificate market. They now offer hosts the ability to sell the certificates directly to the customers. Those certficates auto install. Yes, the hosting company does not have to do one thing! They can make some money off of these sales with commissions (though if you are a host, realize you have to ask to get your share after 30 days, no automation there!)

So here's what cPanel says about those extra htaccess lines:

"We now automatically update .htaccess files to exclude DCV checks from HTTP redirections so that the DCV checks complete successfully. This resolves issues that some customers reported with AutoSSL and cPanel Market purchases." (source)

This was put into the release notes for version 60 a few months ago but it wasn't spelled out til that forum post. It may have ramifications for your website and may not. I promise you though, you do not want to have your hosting company trying to trouble shoot your secure certificate problems later. Just do it!

So I'm going to bite the bullet here and give you full instructions on how to set up the www switch and the https switch in cPanel. (One of the reasons I do this kind of tutorial is for myself by the way. I make sure I've done it right first and then end up with documentation for later if I forget a detail.)

To start with – you do need to know if that htaccess file is already in existence and what it contains. You can find it in the File Manager and choose to view it in place. If it contains "stuff" you don't recognize, stop and contact your hosting company support. Don't go fooling with existing htaccess code. Oh, and by the way, don't expect your hosting to understand any of it either. Hopefully, you'll luck out and get someone on the line that can read it and work with it. Don't expect them to, though. It's not something that's a standard skill set.

www or non www with https

It can be just this simple though I find this does not always work completely. In this case it's also redirecting non secure to secure. If you aren't doing secure yet, just drop the s off of the https in the following:

1. Choose the specific domain you are working with

2. Enter the url (with https or http)

3. Choose "Redirect with or without www"

 

 

 

 

 

But, on cPanel servers, if you want to edit the files yourself and get a cleaner and possibly better solution. Just paste the lines below into your htaccess file. If you don't know how, just Google how to edit .htaccess in cpanel file manager. 

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Author: Delia Wilson Lunsford, Founder & CEO, WizTech, Inc.