1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

htaccess w/ variables?

Discussion in 'Apache' started by Sanzbar, Jul 10, 2005.

  1. #1
    Here's my question....let's say I have a several subfolders....

    http://www.nothing.com/subfolder/link.html

    http://www.nothing.com/subfolder2/link.html



    In each case, I'm trying to get .htaccess to rewrite the file with the subfolder involved...so for example...


    link-subfoler.html
    link-subfolder2.html

    This isn't hard to go in and manually type out the name of every sub folder, however, when I start doing a lot of subfolders, it gets tedious...


    Is there a way that htaccess can read part of the URL and use the subfolder part of it as part of the rewrite?

    That would save me a ton of time! Thanks!
     
    Sanzbar, Jul 10, 2005 IP
  2. KingSEO

    KingSEO Peon

    Messages:
    295
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like..

    RewriteRule ^/(.*?)/(.*?.html)$ file.php?sub=$1&file=$2

    Havn't tried the above, but unless im totally of the track it should give you an idea.
     
    KingSEO, Jul 10, 2005 IP
  3. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but I don't think that's what I'm looking for...this would be the pseudo code....


    Set variable 'sub' in htaccess for current subdirectory name

    the rewrite like so....
    domain.com/filename-sub.html
    domain.com/filename2-sub.html
    domain.com/filename3-sub.html

    I know how to do rewrites, but I'd like htaccess to be able to detect the current subfolder name and then use it in several of the rewrite urls...
     
    Sanzbar, Jul 10, 2005 IP
  4. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Sounds like you need something like this, my syntax may be off a bit, but to give you a general idea:

    RewriteRule ^/(.*)-(.*).html$ /$2/$1.html

    This is using regular expressions which can be difficult to grasp sometimes. When you surround a match with parenthesis, it takes that as a variable - the first set of parens is $1, the second set is $2 and so on.

    You have a couple of options for troubleshooting what you are trying to write - 1) check your error log. There should be a 404 error when it doesn't work. 2) enable logging for mod_rewrite with RewriteLog - but this has to be done in a conf file and is not allowed in .htaccess.

    Mod Rewrite only rewrites urls as they come into the server, it does not change the output of files when they come from your server. So if you have a page with a link in it, mod_rewrite will not change the link. For some reason I thought you might be confused on this issue, although reading back through you posts I'm not sure why.

    You can google for help with regular expressions, there are a ton of very helpful sites out there. If you are going to be doing a lot of it, I suggest grabbing O'Reilly's reg-ex pocket reference.
     
    nevetS, Jul 10, 2005 IP
  5. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay. So I understand what the above expression does....takes a url, and rewrites it to a subfolder and file.....is there a way to do the exact opposite?

    so, take the subfolder and file, and then write the url on that info?
     
    Sanzbar, Jul 10, 2005 IP
  6. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #6
    Just switch it around

    Rewriterule ^/(.*)/(.*).html /$2-$1.html

    Again, I'm not sure if my syntax is correct, you may have to tweak it a little. I don't do rewrites very often. Maybe you have to escape the middle / by replacing it with \/
     
    nevetS, Jul 10, 2005 IP
  7. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I got it to work!!! Thanks a ton!!!

    I know it sounds silly, but this will be huge.
     
    Sanzbar, Jul 10, 2005 IP
  8. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #8
    what was the final syntax?
     
    nevetS, Jul 10, 2005 IP
  9. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Here's what worked....

    RewriteRule ^([a-zA-Z]*)-(.*).html /$2/fix.php?state=$1 [L,NC]

    I'm still having problems when the filename has a dash....ie

    in other words, 'file-subfolder.html' works, but 'file-name-subfolder.html' doesn't...


    i'm trying this, but it doesn't work...

    RewriteRule ^([a-zA-Z]*)-([a-zA-Z]*)-(.*).html /$3/fix.php?state=$1&second=$2 [L,NC]


    any ideas?
     
    Sanzbar, Jul 10, 2005 IP
  10. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #10
    Not off the top of my head, but I believe Apache uses PCRE (Pearl Compatible Regular Expressions) and you are probably going to need to do a little research with lookahead or lookback validation.

    Basically, you want to look for 2 dashes in the url and if there are 2, then group everything up to the second dash in the first match.
     
    nevetS, Jul 10, 2005 IP
  11. Sanzbar

    Sanzbar Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I appreciate the help.

    Is there a way to store string data into a variable with Regular Expressions?
     
    Sanzbar, Jul 10, 2005 IP
  12. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #12
    nope, it's all based on your matching expressions. It seems limited at times, but whenever I find a limitation a month later I find out that I was just attacking the problem the wrong way. If I run accross the lookahead docs I was reading a few weeks ago, I'll pass them along here.
     
    nevetS, Jul 10, 2005 IP