Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts

    Help with Mod Rewrite

    How exactly would you use Mod Rewrite to change something like this:

    show_post.php?post_id=2

    into this:

    show_post/post_id/2?



    Keep in mind that I have never used Mod Rewrite before, so I don't even know how to begin. All I know is that my host does support it.
    Last edited by Joseph Witchard; 11-10-2008 at 05:13 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You'd start with an .htaccess file. Then, proceed like so:
    Code:
    ; Turn on rewriting
    RewriteEngine on
    ; Provide a rewrite rule, and map it to another location
    RewriteRule ^show_post/([^/\.]+)/([^/\.]+)/?$ show_post.php?$1=$2
    The logic is the characters: show_post followed by a / followed by one or more characters that are not / or . followed by a / followed by one or more characters that are not / or . followed by a possible /.
    I used to have an awesome tutorial I found on the web, but now I resort to the apache documentation for it. There are ending... flags I guess I'd call them, that are similar to iptable chains. Using something like [L] says this is the last rule you need to match so just terminate from here. This way if other similar things match you can control how it flows. These are by no means necessary however, there is always ordering that can be used to control them.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Joseph Witchard (11-10-2008)

  • #3
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    Okay, I'm not doing something right. Since I don't have show_post.php up yet, I decided to try it out in my admin area. Here is my code:

    Code:
    ErrorDocument 404 http://www.uhrebirth.com/error.html
    RewriteEngine on
    RewriteRule ^edit_user/user_id/([^/\.]+)/([^/\.]+)/?$ edit_user.php?$1=$2
    It's not working. The admin area is in a different directory, so would that mess it up? Even though I put the .htaccess file in the same directory? Also, I'm using https to access those pages, so would that mess it up?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Likely too many criteria (wants edit_user/user_id/something/something). Since you know what the querystring is built like you can just code it in. I'd do the same with the first one too, it was just an example since I didn't know what you're querystring should look like:
    Code:
    RewriteRule ^edit_user/user_id/([^/\.]+)/?$ edit_user.php?user_id=$1
    Used with edit_user/user_id/14 mapped to edit_user.php?user_id=14. If its a number you can probably get away with \d+ instead of looking for not / or ..
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Joseph Witchard (11-10-2008)

  • #5
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    Still not working

    Current code:

    Code:
    ErrorDocument 404 http://www.uhrebirth.com/error.html
    RewriteEngine on
    RewriteRule ^edit_user/user_id/([^/\.]+)/?$ edit_user.php?user_id=$1

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Are you certain its a problem with rewriting?
    The code works for me. The only possibilities is that edit_user.php doesn't exist, edit_user.php doesn't handle a user_id querystring, or that you don't have mod_rewrite.
    I assumed though that you did this through .htaccess files, is it possible that its configured from another location (such as a control panel of some sorts)?

    Edit:
    I also assumed you were using apache, not IIS. If you're on IIS, I don't think there is a rewrite but I could be mistaken (maybe check with Brandoe, he would probably know)
    Last edited by Fou-Lu; 11-10-2008 at 03:06 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Joseph Witchard (11-10-2008)

  • #7
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    My host has apache and mod_rewrite on their servers, yes. And I'm not quite sure what you mean by a user_id querystring. If this is what you mean, here's how it works: my database provides me a link to edit_user.php with every user_id there is. I select one of those links, and edit_user.php uses $_GET to get the correct id and thus allows me to edit the correct user.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Joseph Witchard View Post
    My host has apache and mod_rewrite on their servers, yes. And I'm not quite sure what you mean by a user_id querystring. If this is what you mean, here's how it works: my database provides me a link to edit_user.php with every user_id there is. I select one of those links, and edit_user.php uses $_GET to get the correct id and thus allows me to edit the correct user.
    Yeah, thats correct.
    This means when you make use of edit_user.php, you attach to it with edit_user.php?user_id=14 for example, where it then brings up a form or whatever for that user. The above rewrite will let you attach to it with edit_user/user_id/14.

    I should make note of this as well, in case the confusion is coming from the other side of the coin. Mod_rewrite does not change you're html code, it only changes what can be provided in the url to attach to this page. So, when you add an <a href="edit_user.php?user_id=14"> it will still use that url. Which is where rewrite handling can become quite ugly.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Joseph Witchard (11-10-2008)

  • #9
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    Okay, well, I tried to access the re_write url directly by typing it in on my browser, but it just took me to my 404 error page.

    I'm going to be making more investigations into my host as to why this isn't working, but in the meantime, how to you change the HTML to reflect the new url?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You would physically change the links. So, if you had a link like: <a href="page.php?action=something"> you would change it to <a href="page/action/something">. I don't really recommend doing this though, as it now damages reusability of you're code (rewrite would need to be on to make this work).
    You could get PHP to do this for you though, which is the better solution. You're stuck in the same boat if you change all you're urls over, but with PHP you can make you're rewrite identify itself (in that, you can get it to tell you that it has been rewritten by providing a new variable). With this, you can conditionally replace you're urls. This seems like a lot of work to conditionally replace you're urls...
    I'll have to think about this one. I don't normally replace outgoing links (and by that I mean, I don't think I've ever replaced outgoing links), but I can't see it being that bad. Off the top of my head, the idea would be to create a rewriting wrapper object or function for PHP (assumed PHP from you're script extension), and use output buffering to capture the contents. Then pick it apart for its url and replace it accordingly. Maybe its time I made a new snippet script for that
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Joseph Witchard (11-10-2008)

  • #11
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    So, like, I would have to edit each url manually? If I had a while loop pulling information out of the database and giving links to each piece, I would have to re-place each one manually?

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh no, if you're looping to generate them you just change what the format is.
    For example:
    PHP Code:
    while ($row mysql_fetch_assoc($records))
    {
        echo 
    '<a href="edit_user.php?user_id=' $row['user_id'] . '">Edit ' $row['username'] . '</a>';
        
    // Would become
        
    echo '<a href="edit_user/user_id/' $row['user_id'] . '">Edit ' $row['username'] . '</a>';

    I wouldn't do it though. Like I said, I'll see if I can make a wrapper to do this (or find an easier solution).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Joseph Witchard (11-10-2008)

  • #13
    Regular Coder
    Join Date
    Oct 2008
    Posts
    255
    Thanks
    113
    Thanked 0 Times in 0 Posts
    Okay. Now, I don't have a edit_user/user_id file in my website. Is that where I'm going wrong? Does that file need to actually exist in order for this to work? If so, what code does that file need to contain?

    EDIT: Got it working, so no worries. Thanks
    Last edited by Joseph Witchard; 11-10-2008 at 05:12 AM.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Nope, you don't need it if you're mod_rewrite is working.
    Here, I'll show you what I did to test mine:
    Structure:
    Code:
    /
     +- .htaccess
     +- edit_user.php
    edit_user.php
    PHP Code:
    <?php

    if (isset($_REQUEST['user_id']))
    {
        print 
    $_REQUEST['user_id'];
    }

    ?>
    .htaccess
    Code:
    RewriteEngine on
    RewriteRule ^edit_user/user_id/(\d+)/?$ edit_user.php?user_id=$1
    Url: http://127.0.0.1/edit_user/user_id/27
    Output: 27
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •