View Full Version : Extreme Mod rewrite for clean URLS?
duncanhall
April 23rd, 2007, 08:14 AM
I've read through Mirans new article on this, and it was definately useful, but not quite what I'm looking for.
I'm developing a site for a college, which will cater for around 100 students. At the moment, each of them has a portfolio page which is accessed with something like this:
portfolio.php?student=34
where the 'student' variable is obviously used to extract their details from a database. Using foodpks article, I could redirect
"college.com/portfolio/34" to "college.com/portfolio.php?student=34"
which is lovely, but what I really want to be able to do is redirect something like
"college.com/portfolio/jimmy_johnson"
to
"college.com/portfolio.php?student=34"
Is there any sensible way of being able to do this, without having to use the student names to access the details?
foodpk
April 23rd, 2007, 08:39 AM
Yo! It's me! Ha!
By the way, sorry I didn't reply to your previous thread about the www redirect, but that problem is just really mind boggling, I've tried it on my home server and keep encountering the same problems; the rewrite *should* work fine, but it doesn't.
As to your current problem, it's kind of tricky. Because the URL rewriting is done before the server calls the page or any kind of server side scripting language on it.
And Apache doesn't really have any kind of its own scripting language that could look up what Jimmy Johnson's ID is in the database and then replace that.
I think that the best and simplest way to approach this (most sites that behave like that use this method) is to take the $_GET['student'] variable (which would equal, say, "jimmy_johnson" or "jimmy-johnson"). And split that into a $firstname and $lastname.
$fullname = $_GET['student'] //this is "jimmy_johnson"
$name_array = explode("_",$fullname);
$firstname = $name_array[0] //jimmy
$lastname = $name_array[1] //johnson
And then when you want to get the data about that student, use an SQL statement similar to:
$query = "SELECT blah, bar, ... foo FROM students WHERE students.firstname = '$firstname' AND students.lastname = '$lastname'
That solves the matter in a very simple and elegant fashion.
duncanhall
April 23rd, 2007, 08:53 AM
Thanks for the help, that's pretty much what I thought I'd have to do. I was a little sceptical about using names to access details, as there's room for duplicates if you aren't using unique values. Having said that, this is a one time showpiece for the students, so there won't be any additions to the DB in the future. meaning I can probably get away with it.
Before I try and shuffle things around for that, would you mind having a look at this:
RewriteEngine on
RewriteBase /dev/
RewriteRule ^students/([0-9][0-9])$ students/$1/ [r]
RewriteRule ^students/([0-9][0-9])/$ portfolio.php?student=$1
This is .htaccess file I have just set up to direct "students/35/" to "portfolio.php?student=35". At the moment it works fine, but I feel like I'm missing something as I don't really have much experience with Apache. What does the "FollowSymLinks" option do exactly?
As for the other redirect problem, thanks a lot for trying, I'm hoping once the site is moved from the /dev folder to /root that the problem will just fix itself. Wishful thinking I guess, but I shall have to wait and see.
Thanks again for the help.
foodpk
April 23rd, 2007, 09:07 AM
The FollowSymLinks is neat to have, but not really crucial. It just tells Apache that if a file you've told it to access is not a file per se but a symbolic link (a file that points to a file somewhere else) to follow it and display the file it points to. Sometimes symlinks come in handy, for instance if you want a folder to have two names.
Hmm, the thing with your rewrite rules would be that you use two unnecessarily instead of one and that it only allows two digits. If you get to the 100th student, it won't redirect. I'd use something like
RewriteEngine on
RewriteBase /dev/
RewriteRule ^students/([0-9]+)/?$ portfolio.php?student=$1 [QSA]
The + means it matches one digit or more, the question mark after the slash means that it doesn't matter if there's a slash or not. I also added QSA because it's always handy to add that, so that you have the freedom of appending query strings if you ever feel the need to.
duncanhall
April 23rd, 2007, 12:03 PM
Cheers for that, it's all starting to make more sense now. I'm even starting to enjoy all this Apache palaver.
I now have the following .htaccess file
RewriteEngine on
RewriteBase /dev/
RewriteRule ^students/([A-Za-z\_\-\']+)/?$ students.php?student=$1 [QSA]
This is designed to redirect:
From "students/winston_smith/" to "students.php?student=winston_smith"
It seems to be working perfectly, but again I just want to make sure I'm not going to do anything that might cause my server to explode in frustration.
The underscore is included in the regex match as it's used as a delimiter between the names. But I've also had to include the hyphen(-) and the comma('), because these are present in some of the names in the database.
Can you see any problems that mgiht arise from the inclusion of these characters in the rewriteRule?
foodpk
April 23rd, 2007, 12:27 PM
I don't think there's really anything in the rewrite rule that may cause you problems. That's the magic of it, the rule itself is really simple.
Underscores and dashes aren't really a problem and single quotes shouldn't be either, really, if you take care to filter the data that you receive from GET. You know, carefully escape the single quotes before doing queries and display appropriate information if the script doesn't find anyone that matches to the name in the URL.
But otherwise, I think everything should work like a charm, just keep it stupid and simple :D
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.