Forum Moderators: phranque

Message Too Old, No Replies

Can you insert php code into a .htaccess file?

         

smithaa02

9:57 pm on Apr 2, 2007 (gmt 0)

10+ Year Member



I would like to insert PHP code into a .htaccess file in order to set up dynamic PHP driven RewriteRule's. So for example /products/booktitle would translate into /products/books.php?id=3, products/anotherbooktitle would translate into /products/books.php?id=4, etc...

Any ideas?

jdMorgan

10:30 pm on Apr 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, .htaccess is a server config file, executed in the URL-to-filename translation phase of the API, not a script executed in the content-handling phase.

Use RewriteMap in httpd.conf to call a PERL script, do a database lookup and return the new URL to mod_rewrite in httpd.conf or .htaccess.

See RewriteMap in the mod_rewrite documentation.

Alternately, simply pass the unchanged "booktitle" in the URL to your books.php script, and let the script extract it. In other words, instead of reading "id=3" in books.php, just get the REQUEST_URI and extract "booktitle" from it. You'd only need to use a rewrite rule to rewrite *all* /products/<anything> URLs to this script, and let the script deal with extracting and determining the meaning of the <anything> part of the URL. Also see Apache 2.x AcceptPathInfo.

Jim