...

View Full Version : Help with Mod Rewrite



Joseph Witchard
11-09-2008, 08:01 PM
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.

Fou-Lu
11-09-2008, 08:56 PM
You'd start with an .htaccess file. Then, proceed like so:


; 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.

Joseph Witchard
11-10-2008, 12:07 AM
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:


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?

Fou-Lu
11-10-2008, 12:48 AM
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:


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 ..

Joseph Witchard
11-10-2008, 01:27 AM
Still not working:(

Current code:


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

Fou-Lu
11-10-2008, 02:03 AM
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)?


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)

Joseph Witchard
11-10-2008, 03:15 AM
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.

Fou-Lu
11-10-2008, 03:22 AM
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.

Joseph Witchard
11-10-2008, 03:35 AM
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?

Fou-Lu
11-10-2008, 03:52 AM
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 ;)

Joseph Witchard
11-10-2008, 03:58 AM
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?

Fou-Lu
11-10-2008, 04:02 AM
Oh no, if you're looping to generate them you just change what the format is.
For example:


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).

Joseph Witchard
11-10-2008, 04:09 AM
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 :)

Fou-Lu
11-10-2008, 04:17 AM
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:


/
+- .htaccess
+- edit_user.php


edit_user.php


<?php

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

?>


.htaccess


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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum