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.

Mod Rewrite - rewrite rule

Discussion in 'Apache' started by Weirfire, Sep 8, 2005.

  1. #1
    Back to this stuff again :(

    Just wondering if I wanted to extract 2 values from the URL

    e.g.

    Using the url domain.com/a/b/ typed into the browser I actually want it to call

    domain.com/x.php?var1=a&var2=b

    When I run my rewrite rule all I get is domain.com/x.php?var1=a/b/&var2=

    Any ideas how to split it up?



    My rewrite rule is;
    SEMrush
     
    Weirfire, Sep 8, 2005 IP
    Willy likes this.
    SEMrush
  2. piniyini

    piniyini Well-Known Member

    Messages:
    511
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    170
    #2
    RewriteRule ^(.*)/(.*)$ /x.php?a=$1&b=$2 [L]

    Try that
     
    piniyini, Sep 8, 2005 IP
    Weirfire and Hodgedup like this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thanks piniyini but still geting a=$1/$2

    hmmm should there be anything else in between ^(.*)/ and (.*)$ ?
     
    Weirfire, Sep 8, 2005 IP
  4. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Problem is the first .* is too "greedy". But the fix is simple as pie ;)

    RewriteRule ^([^/]+)/(.*)$ /x.php?a=$1&b=$2 [L]
    Code (markup):
    The "([^/]+)" will match (one or more) characters, up till it encounters a forward slash.
     
    Willy, Sep 8, 2005 IP
    Weirfire likes this.
  5. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #5
    If the script is in the same directory as the .htaccess file, take off the / in /x.php. The RewriteBase line covers that.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)/([^.]+)/$ x.php?a=$1&b=$2 [L]

    domain.com/whatever/whatever/ (Take out the / before $ if you don't want the / at the end of the URL.)
     
    Nintendo, Sep 8, 2005 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    Thanks guys. Piniyini has been giving me a bit of a guided tour of mod rewrites and I've not been doing things completely the right way round. Words like "no! bad no no " and "smack" come to mind. I was trully made to feel like the grasshopper.

    Thanks for your help Piniyini :)
     
    Weirfire, Sep 8, 2005 IP
  7. Willy

    Willy Peon

    Messages:
    281
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Willy, Sep 8, 2005 IP
  8. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #8
    Thanks Willy.

    Just skimmed over that and it looks like a pretty good resource :)

    You guys have all been a lot of help with this stuff and I appreciate it. I suppose for you lot this query is pretty simple.
     
    Weirfire, Sep 8, 2005 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    Now I have come across probably the simplest mod rewrite rules yet and cant seem to get it to work the way I want.

    What I want to do is turn

    domain.com/this-text

    into

    domain.com/page.php?a=this-text



    This is my code so you can tell me how wrong I was

    
    <IfModule mod_rewrite.c>
    Options +Indexes
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteRule /(.*)$ page.php?a=$1 [L]
    </IfModule>
    
    Code (markup):
     
    Weirfire, Sep 12, 2005 IP
  10. dct

    dct Finder of cool gadgets

    Messages:
    3,134
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #10
    I'd do
    
    RewriteEngine On
    RewriteRule ^(.*)/?$	page.php?a=$1
    
    Code (markup):
    Like I've said before I'm not an expert in this but can hack like the best of em till it works :)
     
    dct, Sep 12, 2005 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11
    I can usually hack them till they work as well but this rewrite stuff is really giving me a headache.

    Thanks dct. I'll give it a try. :)
     
    Weirfire, Sep 12, 2005 IP
  12. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #12
    I think my problem is that when it's rewriting it keeps on rewriting.

    What it seems to be doing is setting a to page.php so the URL is effectively

    www.domain.com/page.php?a=page.php instead of ?a=this-text


    Should I write a condition for page.php?
     
    Weirfire, Sep 12, 2005 IP
  13. piniyini

    piniyini Well-Known Member

    Messages:
    511
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    170
    #13
    i think he's got it figured out
     
    piniyini, Sep 12, 2005 IP
  14. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #14
    Yeah, it's fine. I just made a silly mistake heh :)
     
    Weirfire, Sep 12, 2005 IP
  15. dct

    dct Finder of cool gadgets

    Messages:
    3,134
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #15
    Once upon time there was mod rewrite and Weirfire,
    A wedding and 4 threads later,
    They all lived happily ever after.
    The End (or is it)
     
    dct, Sep 12, 2005 IP
  16. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #16
    There's never an end to Weirfires book... at least not till I'm 75 ;)
     
    Weirfire, Sep 12, 2005 IP
  17. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #17
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^([^.]+)$ get-section.php?rewrite=$1 [L]
     
    Nintendo, Sep 12, 2005 IP
  18. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #18
    Would that not cause a recurrence on the rewrite rule as well?
     
    Weirfire, Sep 12, 2005 IP
  19. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #19
    *gulp* In mod_rewrite, I don't even know what a recurrence is!! :D

    I think any URL on the site would point to get-section.php?rewrite=ANYTHING

    So only if every URL on the site originally has get-section.php?rewrite= in it should that code be used! Adding a unique extionson can make it work if there are URLs you don't want changed.
     
    Nintendo, Sep 12, 2005 IP
  20. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #20
    The only reason I say is I've tried something similar to that before and I got get-section.php?rewrite=get-section.php

    Because the rewrite rule looks for anything at the start it keeps rewriting the URL once it's rewritten. This is the way I see it anyway and is the reason I was asking if you could do write a condition to stop it looping if the URL was the format of the rewritten URL.
     
    Weirfire, Sep 12, 2005 IP