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.

Accepted 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.

Written by Gumbo
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki