Mod-Rewrite

Discussion in 'PHP' started by NaSh123, Jan 2, 2008.

  1. #1
    I was wondering if there is a way I can distinguish between letters and numbers.

    For example I have

    RewriteRule ^the-directory/(.*)/(.*)$ index.php?app=the-directory-cat&id=$1&l=$2 [L]

    How can I split this up so I can have...

    RewriteRule ^the-directory/(.*)/(LETTERS)$ index.php?app=the-directory-cat&id=$1&l=$2 [L]

    RewriteRule ^the-directory/(.*)/(NUMBERS)$ index.php?app=A-directory&id=$1&l=$2 [L]

    So basically letters will go to a different page and numbers will go to a different page?

    Thanks!
     
    NaSh123, Jan 2, 2008 IP
  2. lslars31

    lslars31 Peon

    Messages:
    260
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Shoot... i'm not sure that you can do that with mod-rewrite. You could rewrite it to a different php page that would include one of two files based on whether it had letters or numbers.
     
    lslars31, Jan 2, 2008 IP
  3. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Would it work doing something like

    RewriteRule ^the-directory/(.*)/([\d]+)$ index.php?app=the-directory-cat&id=$1&l=$2 [L]

    RewriteRule ^the-directory/(.*)/([\D]+)$ index.php?app=A-directory&id=$1&l=$2 [L]

    I have this setup now but I don't see anything happening or working.
     
    NaSh123, Jan 2, 2008 IP
  4. trevHCS

    trevHCS Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I don't think you can use those kinds of Perl reg-ex codes, but something like this should work:

    RewriteRule ^the\-directory/(.*)/([0-9]+)/?$    /index.php?app=the-directory-cat&id=$1&l=$2 [L]
    
    RewriteRule ^the\-directory/(.*)/([a-zA-Z]+)/?$   /index.php?app=A-directory&id=$1&l=$2 [L]
    Code (markup):
    That would catch URLs such as these:

    www.example.com/the-directory/xxx/123
    www.example.com/the-directory/xxx/ABC

    ...and pass the xxx as id= and the numbers or letters as l=


    To make things more complete:

    - Added a "\-" as "-" is kindof reserved in reg-ex although sometimes it seems to work fine, but other times won't catch a hyphen. Of course if this is just an example then ignore that.

    - Added a "/?" which says this might contain an ending slash on the directory as you can never be sure how people will link to it.

    - Added a / before index.php just so it knows to run the one in site root. You might not of course want that depending where the file is actually located.


    If that still doesn't work, can you give us an example of what the URL will look like that's trying to call this?

    Trev
     
    trevHCS, Jan 2, 2008 IP