#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    28

    Rewrite to different kinds of php conditions


    How would I got about using mod_rewrite to rewrite:

    www.domain.com/index.php?home to www.domain.com/home

    but also have
    www.domain.com/index.php?profile=John to www.domain.com/John
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,756
    Rep Power
    9397
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    28
    Originally Posted by requinix
    What if someone uses the name "home"?
    That won't be permitted, we'll have a blocked list. Basically the users url will be in the DB, and that will be used to pull their profile. When someone chooses their url, it'll be checked for the words we're using to make the site work, and then throw an error if it matches one of those.
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,906
    Rep Power
    3969
    Originally Posted by HDFilmMaker2112
    That won't be permitted, we'll have a blocked list.
    What about future expansion, like if someone is using the name 'contest' and you wanted to use that as the slug for some new contest your hosting?

    It would be better to separate them somehow, such as doing /profile/John for user profiles, or /p/John if you want it shorter.

    If you do still want to go with that setup though, you might look into RewriteMap, or just rewrite all the urls to something like index.php?slug=blah and inside index.php decide if it is a profile name or a page name.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    28
    Originally Posted by kicken
    What about future expansion, like if someone is using the name 'contest' and you wanted to use that as the slug for some new contest your hosting?

    It would be better to separate them somehow, such as doing /profile/John for user profiles, or /p/John if you want it shorter.

    If you do still want to go with that setup though, you might look into RewriteMap, or just rewrite all the urls to something like index.php?slug=blah and inside index.php decide if it is a profile name or a page name.
    Looking in to Rewrite Map... I came up with this.

    Code:
    RewriteMap pages txt:/pagesmap.txt
    RewriteRule ^([A-Za-z0-9_]+)$ /index.php?${pages:$1|NOTFOUND} [PT]
    #At this point wouldn't everything be sent to index?page_name_here or a 404 page?
    RewriteMap profile txt:/profilemap.txt
    RewriteRule ^([A-Za-z0-9_]+)$ index.php?profile=${profile:$1|NOTFOUND} [PT]
    How would I add in the profile call though? I guess I need some kind of Rewrite Condition, but I have no idea how to do that. I would need a condition that says if the first rewrite rule returns a NOTFOUND go to the second Rewrite Rule.

    EDIT:
    What about this:

    Code:
    RewriteMap pages txt:/pagesmap.txt
    RewriteRule ^([A-Za-z0-9_]+)$ /index.php?${pages:$1|profile=$1} [PT]
    The problem here is I have an else statement in my pages which show up as the default page if the value doesn't match any of the if/elseif statements.
    Last edited by HDFilmMaker2112; June 18th, 2012 at 10:23 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    199
    Rep Power
    28
    Now that I'm thinking about it some more, redoing the if/else statements with a matching condition probably does make sense:

    I would just have to make this:
    PHP Code:
    /*Account Settings Page*/
    elseif($_GET['settings']=="account"){
    $switch="content";
    $content.='
    Account Settings
    '
    ;
    }
    /*Privacy Settings Page*/
    elseif($_GET['settings']=="privacy"){
    $switch="content";
    $content.='
    Privacy Settings
    '
    ;
    }
    /*Settings Home Page*/
    elseif(isset($_GET['settings'])){
    $switch="content";
    $content.='
    Settings
    '
    ;

    into something like this:

    PHP Code:
    /*Account Settings Page*/
    elseif($_GET['p']=="settings" && $_GET['edit']=="account"){
    $switch="content";
    $content.='
    Account Settings
    '
    ;
    }
    /*Privacy Settings Page*/
    elseif($_GET['p']=="settings" && $_GET['edit']=="privacy"){
    $switch="content";
    $content.='
    Privacy Settings
    '
    ;
    }
    /*Settings Home Page*/
    elseif($_GET['p']=="settings"){
    $switch="content";
    $content.='
    Settings
    '
    ;

    Then rewrite to /settings and /settings/account and /settings/privacy

    Doing that it should be as simple as the following, right:

    Code:
    RewriteRule ^([A-Za-z0-9_]+)/?([A-Za-z0-9_]+)?/?$ /index.php?p=$1&edit=$2
    That should match anything between a-Z and 0-9 with an optional / and an optional second "folder" with an optional /.
    Last edited by HDFilmMaker2112; June 18th, 2012 at 10:58 PM.

IMN logo majestic logo threadwatch logo seochat tools logo