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, not just
  • 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 “” in their browser address bar, the site needs to point their browser at  “”. 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…

…must all redirect to 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, which shows you that by having multiple rules to handle an URL like “”, 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:
301 Moved Permanently
301 Moved Permanently
301 Moved Permanently
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, 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, 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), which behind the scenes actually lives on

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’s) .htaccess rules. But my SSL certificate only covers the domain 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 ‘’ or just the bare ‘’. No rewriting ‘’ 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 "" 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} ^$ [OR]
RewriteCond %{HTTP_HOST} ^$
# 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} ^/$
# of the above three conditions must be met to trigger this rewrite rule:
RewriteRule ^(.*)${REQUEST_URI}%{ENV:doRed} [R=301,L]
#So, the second clause says, in all, that: (if the host is "" OR just "") 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, at 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

  • Michael Kupietz
  • FileMaker Consultant
  • Serving clients locally and remotely, in California's San Francisco Bay Area and nationwide
  • Phone: (415) 545-8743
  • Download vCard