Mod_Rewrite Help

Discussion in 'Apache' started by oddcomments, Nov 11, 2008.

  1. #1
    This is too tricky for me to figure out ...not sure it can be done.

    I want my URL structure to look like:
    http://example.com/foo/
    http://example.com/bar/

    and call:
    http://example.com/index.php?page=foo
    http://example.com/index.php?page=bar

    That part's easy, but I want one folder (where I store images, JavaScript, CSS, etc) to be ignored.

    Let's assume it is:
    http://example.com/lib/

    ...and my sub-folders are:
    http://example.com/lib/img/
    http://example.com/lib/css/
    etc...

    Is there a way to do this while ignoring /lib/?
     
    oddcomments, Nov 11, 2008 IP
  2. oddcomments

    oddcomments Peon

    Messages:
    77
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm thinking this might work, but I don't have an environment to test it in. Just a live server. Could someone verify?

    RewriteEngine On
    RewriteBase /
    RewriteRule ^lib/$ lib/
    RewriteRule ^(.*)/$ index.php?page=$1 [L]

    Will Apache stop running rules after the first match?
     
    oddcomments, Nov 11, 2008 IP
  3. oddcomments

    oddcomments Peon

    Messages:
    77
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actually... I remembered I had an unused domain and tested it. My solution was mostly correct. I was just missing an [L]

    Here's the solution if anyone has the same issue:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^lib/$ lib/ [L]
    RewriteRule ^(.*)/$ index.php?page=$1 [L]
     
    oddcomments, Nov 11, 2008 IP
  4. oddcomments

    oddcomments Peon

    Messages:
    77
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey, Odd! Thanks for all the help ;-)
     
    oddcomments, Nov 11, 2008 IP
  5. oddcomments

    oddcomments Peon

    Messages:
    77
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You need to add "RewriteRule ^lib/(.*)/$ lib/$1/ [L]" to include sub-directories under your library ("lib"). Otherwise, those directories will redirect as well.

    The code will look like this.

    RewriteEngine On
    RewriteBase /
    RewriteRule ^lib/(.*)/$ lib/$1/ [L]
    RewriteRule ^lib/$ lib/ [L]
    RewriteRule ^(.*)/$ index.php?page=$1 [L]
     
    oddcomments, Nov 11, 2008 IP