Msg#: 4428076 posted 11:32 am on Mar 12, 2012 (gmt 0)
I currently use the following rule to rewrite all requestes for any htm or html pages in a given url to the new php script. It works fine however I want to change the rule so that instead of just redirecting html or htm pages, it would redirect everything except images, ie .jpg, .gif, .bmp, .png.
Can you help me make this expression work this way round and also make it as efficient as possible?
Msg#: 4428076 posted 9:51 pm on Mar 12, 2012 (gmt 0)
I'd be interested to see the results, but I'm pretty sure you can't use ! in the middle of a pattern. In vanilla Regular Expressions it's only used in lookaheads/ lookbehinds; its broader "not this" meaning is specific to mod_rewrite. Also some other Apache mods, but never mind those.
(([^/]+/)*) = there may or may not be additional directories here. Since you're ultimately looking for something with an extension, you can shave a couple of nanoseconds by expressing the inner bit as [^/.]+/
[^/.]+ = a final bit of plain text excluding directory slashes and periods
\. = literal period
So far it looks really good. It's just that mid-pattern ! that I'm leery about.
:: detour here ::
OK, I went and tested it in my art studio's site which never gets any traffic. I'd actually expected a 500-class error, but all that happened was that the rule didn't execute. I deleted the ! and it worked as intended: that is, it redirected a jpg request to the nonexistent page I'd specified.
But there's got to be a finite number of possible extensions on your "real" files, especially if you've already constrained the search to a particular sub-sub-directory. For example, you don't want to redirect css files do you? (Was going to say robots.txt, but of course it doesn't live in a triple nest of folders ;) )
So the rule will work nicely if you replace the !(jpe?g) et cetera with the desired extensions, say (jsp|php|html?) et cetera.