Passing Variables from PHP to .htaccess

Discussion in 'Apache' started by Jaxo, Jan 2, 2011.

  1. #1
    Hello,

    I have a quick question. How can I pass a variable from my PHP script to my htaccess?

    For example, I have a variable called $testVar. I've set $testVar = "Content". How can I use a mod_rewrite rule to do something like this? I've seen this everywhere, and I've gotten it working, but I need something more.

    "Instead of page.php?id=4, put page/4.htm"

    Instead of page/4.htm, I want to make it page/$testVar.htm, or page/Content.htm.

    Is that possible? And how? Thanks :)
     
    Jaxo, Jan 2, 2011 IP
  2. cipals15

    cipals15 Well-Known Member

    Messages:
    1,085
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    100
    #2
    cipals15, Jan 2, 2011 IP
  3. 00joe

    00joe Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think you mean from your htaccess to php. If so, you would use mod rewrite. For example, this rule takes a condition of a user going to a certain domain and then passing it as a variable

    RewriteCond %{HOST} <somedomain>
    RewriteRule ^(.*) http://myhost/bin/index.php?domain=$1 [P,L]
     
    00joe, Jan 3, 2011 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Simple answer, no.

    By the time PHP knows about the request, the mod_rewrite fules in htaccess have already been processed.
    You'll have to forget about mod_rewrite/htaccess and use the "header()" function in PHP to send a "Location" header in addition to a 301/302 HTTP status code.
     
    joebert, Jan 3, 2011 IP