Wow thanks for the replies! I actually have (to the best of my knowledge) ordered things in my .htaccess that way though I should double-check just in case since I will be merging two different sets of .htaccess commands from the new and old versions of my site.
In regards to
UseCanonicalName will it conflict with
%{SERVER_NAME}?
I spent some time with the code and I'm really really close to figuring this out. What
does work is a second level redirect, in example
example.com/blog/index.php will redirect
example example.com/blog/ and on top of that it works both on localhost and my domain name without having to have two copies of the .htaccess file thanks to
%{SERVER_NAME}.
The only thing the code does not do is effect all level of directories. So in example
example.com/level1/level2/level3/index.php will redirect to
example.com/level1/ when I'd prefer to have it redirect to
example.com/level1/level2/level3/.
So I think the most effective question with keeping in mind that I do have a reasonable grasp of regular expressions is there a way to do a sub-string select with regex/Apache?
In example with PHP...
echo '<div>'.substr('http://example.com/level1/level2/level3/level4/index.php',0,-9).'</div>';
...will output...
http://example.com/level1/level2/level3/level4/ ...regardless of how many levels of directories deep the request is.
Here is what is working in it's entirety minus most of the file paths and file extensions in the initial two lines. I've really reduced it to the absolute minimal amount of code that works (only works with first level directories of course). I do understand some of the operators such as ^ start and $ end though I don't see why all the other bits of code are included unless it correlates back to something like a different server variable that could change this or certain characters won't catch unless they're explicitly declared, etc?
RewriteRule ^(redirect.php|test.php|blog/|forums/) - [L]
RewriteRule !\.(css|cur|gif|gz|html|h3m|ico|jpg)$ index.php
RewriteCond %{THE_REQUEST} index\.php
One thing that has crossed my mind from all of this is having the sub-directory redirect condition placed
before the rule that rewrites everything to the main index.php file. I'm pretty sure it would be necessary otherwise my CMS would have to handle the index.php to / redirects (which I could easily do with PHP if I was able to set that up).
I'm also aware that $1 through $9 can match parts of a regex return (I'm not entirely sure that is stated correctly) though I have a hunch that there may be a way to match everything of
%{THE_REQUEST} except the index.php part of the URL and redirect to it? So are we trying to simply match *
before index\.php? If my understanding of the $ return(?) is correct then it may be as simple as creating a regex that executes that kind of match? I know my post is pretty much all over the place though I'm going to follow my hunches and try some more regular expressions. I know before
this post I did not mention anything about multiple level sub-directories so that may change the whole approach.
I'll try creating a regex to match * before index\.php, I use a free program called
The Regex Coach which is awesome as it has a tool to see step-by-step matching of a regex against a string so I'll work with that and see if I can figure out the formula against the string "level1/level2/level3/level4/index.php" presuming that is what %{THE_REQUEST} contains.
- John