Hi jj4,
All questions are good … hopefully the answers will be, too!
You also know who to blame if you don’t understand something in the tutorial … and where to go for answers.
There is the possibility of multiple redirects … because any redirect (to the same location) will cause the mod_rewrite to be RE-executed (from the start) until there are no further redirections (that is why it’s important NOT to write “loopy code”).
It’s NOT a big deal that mod_rewrite handles its code in a serial fashion because, as stated before, ORDER IS IMPORTANT. All I was doing was taking care of the redirections that would not have to be repeated before getting into the external redirections (http or https).
You’re not being dense. The question is good but shows that you’re not thinking like a computer (logically). I separated my first and second RewriteRules because they handled redirections differently (one adding and one removing a trailing / … POTENTIALLY). I was inclined to combine the third and fourth but did not want to mess with the www and https at the same time (the two RewriteCond statements do not combine logically to arrive at a single solution).
Okay, that was one gotcha for you! Okay, three. Leave the R=301 flags in all four RewriteRules because you may only need one (or two or three) redirections and not match the fourth RewriteRule.
Subdomains: I had interpreted your “specification” to require a leading www. My error. More information is required, then, for the code: How many subdomains are you using (including www)? If you can “list” them in your code (rather than use “catch all” code), and your domain name, yes, you can easily add www UNLESS the subdomain is {in the “list”} using
RewriteCond %{HTTP_HOST}. !^((www|sub1|sub2|sub3)\.)example\.com [NC]
Here I need to add that hackers attempt to break your code to find a way inside your website. The more generic your code, the more opportunities for the hacker to find some combination which can break your code and allow access to your website. In other words, be as specific as you can … and a “list” does that perfectly (not to mention using machine cycles to redirect garbage)!
Personally, I prefer subdomains and domains without www. but that’s just me. That said, if you’re using a secure server, the cert is normally for either the www or non-www as you requested it from the CA so be sure that your mod_rewrite matches!
.? is VERY different than . (dot) as the ? makes it optional. If someone requests just your domain (example.com, www.example.com without without the trailing /), there is NO character for the dot metacharacter to match and {dot} would fail and the RewriteRule not executed (despite all the correct conditions were matched in the RewriteCond statements). In other words, .? will ALWAYS match.
OMG! Is it logical to you to use NINE RewriteRules instead of FOUR? Remember my first admonition: SPECIFY your requirements for the redirections in words BEFORE you start coding. I don’t think that can be said of:
-
Skip three rules if there is a trailing /
-
If not a directory OR {no match, ^/$ is no longer valid as the / cannot be matched - that went away with Apache 1.x), skip two rules
-
If directory (from #2), redirect to secure server without changing the domain but adding a trailing slash
-
Same as #3 but for requests without www.
-
If directory, skip twice
-
Strip trailing / but add www to domain
-
If not www, strip {REQUEST_URI} and send to secure server
-
Add www for domain
-
If not secure server, send to secure server
How convoluted was that!?! IMHO, back to the basics: Handle the directories and files (trailing /'s) then handle the www (with the “list” code rather than “catchall”) and then enforce the secure server (Google does not like it when you redirect secure server requests). Does that not sound more logical than a lot of if … then … else statements and structure?
Don’t worry, you’ll get there! I’m not trying to abuse you, just trying to have you “think” as logically as the mod_rewrite parser does (sequentially) and handling redirections in a logical, orderly manner.
Regards,
DK