.htaccess file is really powerful, but occasionally something of a dark art. Here are some things that I have used over the years, that work (for me anyway) along with hopefully detailed explanations of why they do what they do.
What’s up with the inclusion of
Lots of sites will tell you that
Options +FollowSymLinks is a prerequisite for mod_rewrite, what is less clear is why fortunately for me the answer is clearly stated on: appache.org/mod_rewrite.html.
In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of “/app1/index.html” may result in comparison against “app1/index.html” or “index.html” depending on where the
The directory path where the rule is defined is stripped from the currently mapped filesystem path before comparison (up to and including a trailing slash). The net result of this per-directory prefix stripping is that rules in this context only match against the portion of the currently mapped filesystem path “below” where the rule is defined.
Directives such as DocumentRoot and Alias, or even the result of previous
RewriteRulesubstitutions, determine the currently mapped filesystem path.
So, it’s not actually a prerequisite in all circumstances but in most you’ll find you need it.
For some reason I really struggled to get this to work and initially added it as two separate page rules in cloudflare (cloudflare doesn’t support RegEx in it’s page rules). This did work, but it wasn’t neat and the fact that it wasn’t working properly was irritating me, so I had another go and finally got there.
Here’s the file:
RewriteEngine = On RewriteRule ^feed/?$ https://tamarisk.it/feeds/all.atom.xml [R=301,L]
It redirects anyone coming to https://tamarisk.it/feed to my actual feed location (https://tamarisk.it/feeds/all.atom.xml) regardless of whether they append the trailing slash. It’s a
RewriteRule rather than a
redirect because straight up redirects don’t allow for RegEx so I’m back to needing two rules.
Explanations of bits of the RewriteRule:
- Wrapping the url segment in
$tells the RewriteEngine only to match that bit of the URL. If you miss off the ending
$and the url you’re redirecting to includes the pattern then you end up in an infinite loop.
/?tells it look for either 0 or 1 trailing slash.
R=301be a permanent redirect
Lstop if this rule matches - useful if you want to move from specific to general and don’t want later rules applying.