Forum Moderators: phranque
This was my most recent attempt:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\[ \]]+)/?$ index.php?url=$1 [L]
Aug
If you want to rewrite any-two-directory-levels-followed-by-any-non-blank-nonexistent-filename, then try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/){2}[^/]+)$ index.php?url=$1 [L]
Jim
If you want "ten to twenty" directory levels, then that would be
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/){10,20}[^/]+)$ index.php?url=$1 [L]
Also, we're assuming that you've got other working rules in this file, and therefore have the requisite directive(s) to enable mod_rewrite and the rewriting engine.
As g1smd noted, the php script must check that it does in fact have something specific to serve, and if not, should return a 404-Not found status response. Otherwise, you'll likely have duplicate-content problems with search engines. If a competitor discovers the 'hole,' they can exploit it to intentionally give you duplicate-content problems...
Jim
We're also aware that some 80% of posters don't know the difference between a redirect and a rewrite - even though that's what they are trying to configure their server to do.
So things like the fact that this is not an application for any kind of redirect (but rather for an internal rewrite) matter quite a bit. And the disposition of URLs for which the script can find no appropriate content is critical; We would do you no favor by letting you proceed with generating a "redirect to a 404 page" without comment here, because that could very well sink your site in the SERPs. You might well have left with a pretty little rewrite rule, and been out of business in six months.
The remaining "right aspect of this" is rather trivial. If you want *all* URLs which do not resolve to a physical file to be forwarded to the script, just change the RewriteRule pattern to "^(.*)$". If you want all non-blank URLs which do not resolve to a physical file to be forwarded to the script, change the RewriteRule pattern to "^(.+)$". If you want to make sure that no URL *ending* with a slash (i.e. a directory request) is forwarded to the script, then use "^(.*[^/])$".
Jim
[edited by: jdMorgan at 4:25 am (utc) on June 23, 2009]