.htaccess help with environment variable

Discussion in 'Apache' started by nybbles2byte, Sep 8, 2009.

  1. #1
    I need to set an environment variable to equal part of a domain. In pseudo code it would look something like this:

    m/(.*)\.(.*)\.(.*)$/m
    # subdomain=$1
    domain=$2
    # tld=$3

    Or in other words: given www.abc.com, I want to extract 'abc' and store it in a variable.

    I think something like this is on the right track:

    RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$
    RewriteRule .* - %{E=domain:$2}

    Of course the above doesn't work or I wouldn't be asking for help.

    It would actually be nice to grab all parts with something like:
    RewriteRule .* - %{E=subdomain:$1}.%{E=domain:$2}.%{E=tldomain:$3}
    but for my purposes I just need the middle part.

    Any help greatly appreciated!
     
    nybbles2byte, Sep 8, 2009 IP
  2. nybbles2byte

    nybbles2byte Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Update, I made some progress and can set the env. variable now. The two lines below does it nicely. I only use 'domain' but I thought I would make this more universal.
    RewriteCond %{HTTP_HOST} (.*?)\.?([^.]+)\.([^.]+)$
    RewriteRule ^ - [E=subdomain:%1,E=domain:%2,E=tldomain:%3]

    But the problem comes up when I try and use the variable in code as you can see below...

    This works, the original line of code:
    RewriteCond %{HTTP_HOST} !^((www|reg|stage|svn)\.abc\.(com|net|org))|((abc|svn-abc)\.mywebsite\.com)$ [NC]

    This doesn't, the code with the env. variable in it (one place only for now):
    RewriteCond %{HTTP_HOST} !^((www|reg|stage|svn)\.%{env:domain}\.(com|net|org))|((abc|svn-abc)\.mywebsite\.com)$ [NC]

    Anyone know how to make this work?
     
    nybbles2byte, Sep 9, 2009 IP