I am quickly realizing that I cannot simply copy and paste what is shared here. Many times it just brings the server to it's knees (which is why I am now testing on local dev server using WAMP).
Very true. One must understand the entire 'meaning' of patterns, rules, and entire collections of rules before trying to use the code.
The critical questions are: "Does this do exactly what I want it to do, with my server, my site, my URLs, my directory-structure, and address the specific problem that I want to solve?" and "What effects will visitors and search engines see, and what will this do to the visitor-experience on my site and to my search listings?"
A rule might seem quite simple, but its effects can be quite far-reaching... Ask anyone who's ever unwittingly exposed internal script filepaths as URLs by redirecting after an internal rewrite, and has had to go clean up the mess that that creates! :o
I think my major stumbling blocks are that many times the examples here are for htaccess files whereas I've moved away from them to root httpd.conf files for increased performance and disabled htaccess completely in Apache so it doesn't go looking for those files, so, they just don't work at times.
The major differences are that the URL-paths matched by patterns in RewriteRules located in .htaccess files will be stripped of the path used to get to that /.htaccess file.
So, for example, a rule located in example.com/.htaccess and intended to match a client request for "example.com/foo" will have a pattern of "^foo$" -- Note that the leading slash will have been stripped from the requested URL-path, because that is [art of the path to this .htaccess file. And a rule located in example.com/dir/.htaccess and intended to match a client request for "example.com/dir/bar" will have a pattern of just "^bar$", because again, the path to this .htaccess file will have been stripped from the requested URL-path.
Note that this does NOT apply to the requested URL-paths matched by RewriteConds examining either %{REQUEST_URI} or %{THE_REQUEST}. Those will always contain the full URL-path, starting with a slash.
The same is true in server config files such as httpd.conf, when the code is located inside a <Directory> container -- The URL-path seen by RewriteRules will not contain that part of the path specified in the <Directory> container.
For mod_rewrite code located outside any <Directory> containers, the full URL-path will always be seen by the RewriteRule.
Another difference is that code located in .htaccess is executed during the fix-up phase of the Apache API. Because of this, the mod_rewrite codes behaves recursively. That is, if any rule is invoked, then processing of all rewriterules is re-started. For this reason, it is necessary to explicitly prevent rewrite loops in .htaccess code -- by excluding the target address of the rewrite from the rule that rewrites it, so that the request won't get rewritten over and over again, leading to s 500-Server Error when Apache detects this looping.
So, the bottom line is that code for use in config files should actually be simpler than code for use in .htaccess files, and all that is usually required is to tweak the RewriteRule patterns slightly -- usually just by adding a leading slash. Trivial example:
example.com/.htaccess:
RewriteCond $1 !\.php
RewriteRule ^(.+)$ /$1.php [L]
example.com's httpd.conf file:
RewriteRule ^/(.+)$ /$1.php [L]
And our three most often repeated bits of advice:
Taking into account all mod_rewrite code in all config and .htaccess files, put all external redirects first, in order from most-specific patterns and conditions (one or only a few requested URLs affected) to least-specific patterns and conditions (more or most URLs affected), followed by all internal rewrites, again in order from most- to least-specific. Access-control rules (if any) should precede the redirects where possible, because there is no use wasting server resources redirecting unwelcome visitors.
-and-
Always end each RewriteRule with an [L] flag, unless you have a very good reason not to. Exceptions are very rare.
-and-
Don't forget to delete your browser cache before testing any new server-side code -- config code, .htaccess code, or scripts. Otherwise, your browser may show you stale, previously-cached pages, objects, and server responses.
Jim