Search Engine Optimization: Set up an SSL site to avoid “chained” multiple 301 redirects with .htaccess rewrite rules

Search Engine Optimization: Set up an SSL site to avoid “chained” multiple 301 redirects with .htaccess rewrite rules

TL;DR, for tech geeks: For SEO purposes, I needed multiple rewrite rules in my .htaccess file for this site:

  • Make sure URLs for directories (basically everything except images and external javascript and css files, since this is a wordpress site) always end in a slash.
  • Make sure URLs always redirect to www.kupietz.com, not just kupietz.com
  • Make sure http:// URLs always redirect to https:// urls.

If you completely get it already, and you just want to know how I did it, you can skip down to the part where it says “So. Here’s how I did it“.

Plain English explanation, for everyone else: In other words, if someone entered “http://kupietz.com/contact” in their browser address bar, the site needs to point their browser at  “https://www.kupietz.com/contact/”. Ditto for if any other of “https”, “www” or the trailing slash are missing.

There are a few reasons for this, the primary one being that Google’s Search Console, where webmaster types go to appeal to the Great Cybernetic Demiurge to please put us on the first page of results, considers all the different variations (http://, https://, and with and without the www. prefix) to be separate websites. (I want to add at this point, part of the fun of being in business for yourself today is having to not just be good at what you do, but also having to either be or be able to hire an SEO expert, too, at least if you want your website to ever be seen by potential customers. Anyway…)

From an SEO standpoint, appearing to have multiple websites with similar or duplicate content is considered very bad juju, and will hurt your search engine rankings, and your children’s search engine rankings, and their children’s search engine rankings, and their children’s, and so forth, yea, unto the seventh generation. To steer clear of appearing to Google to be multiple websites with duplicate content, you should pick which URL format you want to use and stick with it, and have every other variation redirect to the corresponding page with the proper protocol (http or https) and either always with the “www.” prefix, or never with it.

Plus, if you happen to on WordPress like this site is, you’re going to get a trailing slash on most of your URLs whether you want them or not, so then that’s got to be in the mix either way.

So, to summarize: in my case, the following url variations…

http://kupietz.com/contact
http://www.kupietz.com/contact
http://kupietz.com/contact/
http://www.kupietz.com/contact/
https://kupietz.com/contact
https://www.kupietz.com/contact
https://kupietz.com/contact/

…must all redirect to https://www.kupietz.com/contact/ to keep things looking neat & clean for Google.

Now, with some simple web searching it’s not that hard to find the rewrite rules to handle all of these possibilities, and if that was good enough, you wouldn’t be here reading this.

The problem is this. You probably found a nice tool like http://www.redirect-checker.org/, which shows you that by having multiple rules to handle an URL like “http://kupietz.com”, your browser actually redirects multiple times to get the job done. So if you’re like me, you googled for the rewrite code to add to your .htaccess file, then, thinking you were finished, ran a test URL through a redirect checker like the above, and saw something similar to this:
http://kupietz.com/contact
301 Moved Permanently
https://kupietz.com/contact
301 Moved Permanently
https://www.kupietz.com/contact
301 Moved Permanently
https://www.kupietz.com/contact/
200 OK

…and immediately clutched your chest at the thought of what Google was going to do to your results ranking for making its spider run back and forth that many times…

…and then, being a perfectionist, said, “There’s got to be a way to do the whole thing with only a single redirect…”

…and immediately started googling. However, unlike me, you didn’t have to spend an entire afternoon googling, because, again unlike me, when you needed to know, someone had already figured it out and had the foresight to make a blog post sharing exactly how they did it.

screen-shot-2016-09-28-at-12-40-20-amAt right is my current result from http://www.redirect-checker.org/, screenshotted just a minute ago as I type this:

Yep. One single redirect to make all three needed rewrites. Now if only there was a redirect to fix their website to put the ‘s’ at the end of ‘congratulations’.

BUT WAIT, THERE’S MORE: there are still more moving parts here that have to be thought about. This site has a few subdomains, like http://bsdetector.kupietz.com, as well as hosting a few secondary sites which are hosted internally as subdomains but have their own domain names pointed at them, like the goofy (and currently broken) http://www.misinformationvisualization.com, which behind the scenes actually lives on http://misinformationvisualization.kupietz.com.

This website runs on a system called Cpanel, which routes all websites, domains, and subdomains on a single IP server through the primary domain’s (in this case kupietz.com’s) .htaccess rules. But my SSL certificate only covers the domain kupietz.com. I can’t have any subdomains (other than ‘www.’) or secondary domains redirected from http: to https:, or anyone visiting those sites will be messed with horrific ways I’ll save discussion of for their own separate blog post. All the above redirect activity has to be only for URLs on ‘www.kupietz.com’ or just the bare ‘kupietz.com’. No rewriting ‘http://bsdetector.kupietz.com’ requests to https, or anything like that. I learned about this the hard way.


So. Here’s how I did it:

But first, a warning: you edit your .htaccess file at your own risk. This post shows what worked for me, but I’m not there to look over your setup for anything that might be different in your case. That’s your job to figure out. If you follow these instructions, and something goes wrong or your website breaks, you’re going to be in a lot of trouble, and it’s not going to be my fault.  If you have a problem with that, you should just leave your .htaccess file alone and hire a consultant to do this for you. By the way, I may be available. :smiley face:

And, of course, back up your .htaccess file before you do this. But if you needed to be told that, you probably shouldn’t be touching it to begin with.

Ok, that said, here is the rewrite rule block that finally worked for me:

#First clause:
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=305,E=doRed:/]
#So, the first clause says: if the part of the URL after "kupietz.com/" has no period in it and no slash at the end, then set an environment variable called "doRed" to '/'
#(I'm actually not sure the "R=305" is necessary, I just had it in there for testing, but this works, so I'm not changing anything!)
#
#Second clause:
RewriteCond %{HTTP_HOST} ^kupietz.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.kupietz.com$
# One of the above two conditions must be met, AND...
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\.kupietz\.com$ [NC,OR]
RewriteCond %{ENV:doRed} ^/$
# ...one of the above three conditions must be met to trigger this rewrite rule:
RewriteRule ^(.*)$ https://www.kupietz.com%{REQUEST_URI}%{ENV:doRed} [R=301,L]
#So, the second clause says, in all, that: (if the host is "www.kupietz.com" OR just "kupietz.com") AND (https is not on, OR the 'www.' isn't there, OR doRed = '/'), then add the https, the www., and the contents of the variable "doRed" to the end, and tell the browser to redirect to the resulting URL.

If you try it, and it works for you, you can thank me, because unless something has changed drastically since I posted this, I just saved you many, many hours of searching. Man, I couldn’t believe how hard it was to get this to work. There are a lot of gotchas.

Credit where credit is due, the post that finally sent me in the right direction was a 7-year-old comment from user jdMorgan on Webmasterworld.com, at https://www.webmasterworld.com/apache/3893795.htm. It still took a lot of trial and error after that, but that’s what finally got me on my way.

You’re welcome. If you go somewhere nice, send me a postcard or something.

Updated Nov. 29, 2016 to include the two lines that prevent Clause #2 from rewriting URLs for subdomains. Oops.


Related articles