mod_rewrite rule question

Discussion in 'Apache' started by Lucky Bastard, Jul 20, 2006.

  1. #1
    I have a rule:
    RewriteRule ^/?([^/]*)\/dir\/([^/]*)$ dir.php?a=$1&b=$2%{QUERY_STRING} [L]

    This works when somebody goes to:
    http://www.mysite.com/yada/dir/abc

    HOWEVER it does not work when somebody goes to:
    http://www.mysite.com/yada/dir/abc/ (note the / after 'abc')

    How does one make a rule accept (but NOT require) a trailing slash.

    Thanks.
     
    Lucky Bastard, Jul 20, 2006 IP
  2. Cryogenius

    Cryogenius Peon

    Messages:
    1,280
    Likes Received:
    118
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Add /? just before the $, which stands for an optional forward slash. You line then becomes:

    RewriteRule ^/?([^/]*)\/dir\/([^/]*)/?$ dir.php?a=$1&b=$2%{QUERY_STRING} [L]

    This will therefore work for URLs with and without the trailing slash.

    Cheers, Cryo.
     
    Cryogenius, Jul 23, 2006 IP