URL rewrite question

Discussion in 'Programming' started by Phoenixx, Jun 15, 2008.

  1. #1
    Hi!

    I'm having a problem with apache mod rewrite, I'm noob as a man can be :D This is the behavior that I need to achieve:

    When someone enters www.mysite.com/gallery/ (with or without trailing slash), it should be redirected to www.mysite.com/index.php?view=gallery and I have solved that part with this rule

    rewriteRule ^([^./]+)/?$ .php?view=$1 [L]

    But, there should also be a possibility for users to directly enter gallery ID with a URL like this one: www.mysite.com/gallery/5/ which then should be redirected to www.mysite.com/index.php?view=gallery&id=5. And that's where my problem is. This is the rule that I'm currently stuck with...

    rewriteRule ^([^/]+/)?([^./]+)/?$ index.php?view=$1&id=$2 [L]

    It works fine except one small thing....as my first parameter I get the name of it but with trailing slash added (for example "gallery/" ), but I'd like to have that parameter without slash at the end. So, is there anyone who could help me out with this one, or I will have to strip that slash in php (which I'd like to escape if possible)

    Thanks for any help!
     
    Phoenixx, Jun 15, 2008 IP
  2. Spawny

    Spawny Well-Known Member

    Messages:
    252
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    110
    #2
    try this
    
    RewriteEngine On
    RewriteRule ^([^/]*)$ /index.php?view=$1 [L]
    RewriteRule ^([^/]*)/([^/]*)$ /index.php?view=$1&id=$2 [L]
    
    
    Code (markup):
     
    Spawny, Jun 15, 2008 IP
  3. Phoenixx

    Phoenixx Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Wow, that was really quick! Thanks man, it works now!! :)

    Rep added ;-)
     
    Phoenixx, Jun 15, 2008 IP