Forum Moderators: phranque

Message Too Old, No Replies

Is it possible to use php variables in .htaccess?

use php in .htaccess?

         

fjpapaleo

11:21 pm on Dec 28, 2005 (gmt 0)

10+ Year Member



OK,this is getting way over my head. I have a re-write that works fine.
RewriteRule ^color-purple-(.*).html$ /color-purple.php?search_fd1=$1

Will write: domain/color-purple-flowers.html.
Not a problem. I can also use this for my products page such as: domain/products-prodID.html

But I also have hundreds of inner pages made up of categories and than sub-categories.

domain/fruits-red-cherries
domain/fruits-blue-blueberries
and than........

domain/vegetables-green-peas
domain/vegeatables-yellow-corn
and so on.........

I basically have about 25 categories and about 12 sub-categories in each. I obviously can't have 300 seperate re-write rules for each one.
Each page uses the variable "$thisPage" such as:
$thisPage="fruits-red.php"
$thisPage="fruits-blue.php" etc..
I was hoping I could somehow use my php variable to create the re-write on the fly but I don't think that's possible. Probably because it looks at the .htaccess first and has no idea what the page is anyway?

The only other thing I can think of would be to create a directory for each category and than place an .htaccess in each directory folder with a re-write for each sub-directory. So I would have abot 12 rules and 25 seperate .htaccess files. That would give me:

domain/fruits/red-cherries
domain/vegetables/green-peas etc.....

Would that be the way to go or is that also too much?
I hope this makes sense, my brain is about fried!

I'm counting on you for this JD :)

jdMorgan

11:48 pm on Dec 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are correct: .htaccess and mod_rewrite are processed in the URL-to-filename translation phase of the Apache API, *before* the content-handling phase is invoked to run php. So you can pass variables from .htaccess to php within the context of a single HTTP request, but not vice versa.

You could put an .htaccess file in each subdirectory. This has the benefit of execution efficiency, but makes modifications and maintenance more difficult.

But you could also likely come up with a minimal set of just a few rules to handle your 300 URLs, using multiple variables in each rule. How you would do this depends on precisely how you want to map URL-strings to script calls and query string name/value pairs, and on how consistent you've been in naming/mapping these URL-string-to-script calls. The single-variable example you provided isn't sufficient to discuss this further, though.

BTW, you can make your existing rule much more efficient:


RewriteRule ^color-purpl[b]e-([^.]+)\.h[/b]tml$ /color-purple.php?search_fd1=$1 [L]

Using a forward-only negative match on "." prevents the regex parser from having to do any 'back-off' passes (5 in this example) to get a match, and will speed processing.

Jim

fjpapaleo

12:14 am on Dec 29, 2005 (gmt 0)

10+ Year Member



Thanks JD! I knew if anyone could answer this it would be you. I'll try it.