[thelist] Mimicing a "normal" directory structure with mod_rewrite

Matt Warden mwarden at gmail.com
Sat Jul 9 09:56:43 CDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

April wrote:
> overlaying one file like categories.php.  I'm fine just doing category/,
> but I can't figure out how to deal with the /'s between subcategories
> and the fact that they might or might not be there.  IE I can do:
> RewriteRule ^dir/([A-Za-z]+)$ category_view.php?cat=$1
> but I can't figure out how to adjust it to deal with a url like:
> http://domain.com/dir/Games/Flash/Free

I'm not sure what you mean when you say they might or might not be
there. The slashes? Or the entire sub category? I'm going to go with
slashes, because I don't want to put a lot work into the latter if
that's not what you want.

To match http://domain.com/dir/Games/Flash/Free and
http://domain.com/dir/Games/Flash/Free/

RewriteRule ^dir/([a-z]+)/[a-z]+/[a-z]+/? category_view.php?cat=$1 [NC,L]

Note the ? quantifier at the end.
http://www.regular-expressions.info/optional.html

It really isn't necessary to pass variables into the querystring like
that, unless you are working on existing code that you don't want to
break. The original request URI is available in the REQUEST_URI server
variable, so you have access to all this information already by simply
passing the request to category_view.php:

RewriteRule ^dir/[a-z]+/[a-z]+/[a-z]+/? category_view.php [NC,L]

(REQUEST_URI within category_view.php would be /dir/Games/Flash/Free)

- --
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFCz+WrAQ0d4HGyPE8RAnTsAJ9U+CX6IRw7fVYBbb9dxUb5DSv8AwCfTDi1
Ht2sApNPxwoIVq/oeDZkKLk=
=lJD/
-----END PGP SIGNATURE-----


More information about the thelist mailing list