R – Avoiding sub-directory request rewrites with Apache mod_rewrite

apachemod-rewriteSecurity

I want to a rewrite rule such that if a user goes to the URL example.org/stuff/junk.jpg the rule will process and end up at re-writer.php but if the user goes to example.org/stuff/hackingisawesome/junk.jpg the rule will not be triggered and they will get a standard 404 (or a page, if one should exist).

I can't tell, based on the environmental variables, if this is possible without some fairly fancy regex.

So does anyone know of either:

a) a way this is already built into the mod_rewrite syntax, or

b) a good, reliable way of handling this with regular expressions?

Links to documentation or tutorials welcome. I'm just feeling clueless on where to go next.

Oh, and I can imagine the ways I could simply have the script that the rule redirects to simply deliver the 404, but I'd rather only use the rule when the conditions exist.

Best Answer

Try this:

RewriteRule ^stuff/[^/]+$ re-writer.php

This will rewrite all requests to /stuff/… with only one additional path segment to re-writer.php.

Related Topic