URL Rewriting Question

Discussion in 'Site & Server Administration' started by keithjameslock, Jan 18, 2010.

  1. #1
    Hi everyone... just wondering what I might add to a .htaccess file to accomplish the following:

    someone visits: /folder-name/
    rewrite to: /index.php?page=folder-name

    someone visits: /folder-name/subfolder-name/
    rewrite to: /index.php?page=subfolder-name

    I figured out a way to do it using a text file as a lookup but I have a feeling that isn't necessary... my guess is that the above can be accomplished with just 1 (maybe 2) lines added to .htaccess.

    Also, can it be written in such a way where "/folder-name/subfolder-name/" would be equivalent to "/folder-name/subfolder-name" ... or is that the default behaviour anyway?

    Thanks!
    ~Keith
     
    Last edited: Jan 18, 2010
    keithjameslock, Jan 18, 2010 IP
  2. keithjameslock

    keithjameslock Peon

    Messages:
    416
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Figured it out... just used this:

    RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?page=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?page=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/index\.php$ /index.php?page=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?page=$2
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /index.php?page=$2
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/index\.php$ /index.php?page=$2

    I used 6 lines to cover all bases but 4 would have been sufficent, 6 might be overkill...
     
    keithjameslock, Jan 18, 2010 IP