Rewriting PHP scripts

New to mod_rewrite? This is a good place to start.

Postby transistor » Thu May 02, 2002 1:32 am

First, very nice work here Brett, really.
Now, what's on my mind.
I have a site that has information on artists, records and songs.
There are three scripts to access such information: artists.php?id=x, album.php?id=x and track.php?id=x
What I would like to have if possible (or the appropiate combination with PHP), a rewrite rule that will 1) when a user enters http://www.ritmopolis.com/myartist, the name of myartist is searched in the database and an id is returned, then sent to the artist.php?id=x and the user sees in the browser http://www.ritmopolis.com/myartist
2) searching for an album would have a similar effect: http://www.ritmopolis.com/myartist/thealbum, the name myartist should be searched and contained in something like aid=x, then the album thealbum should be searched where artist=aid and contained in x, then sent to album.php?id=x and the user should see http://www.ritmopolis.com/myartist/thealbum
3) the same drill for a song...
Can this be done?
So far, I intercept error 404 (because /myartist or /myartist/thealbum don't exist) then search for the text in the database and then use header("location: artist.php?id=$x"), and it works, but I would like to see http://www.ritmopolis.com/myartist instead of http://www.ritmopolis.com/artist.php?id=x
Thanks in advance! :smile:
transistor
 
Posts: 2
Joined: Wed May 01, 2002 4:00 pm
Location: ritmopolis.com

Postby Brett » Wed May 15, 2002 9:54 pm

Well heck, since you're already using a PHP document to intercept the 404 errors, why not just let PHP do all the work?

Instead of redirecting to the document you want, just include it. For example, instead of
header ("location: artist.php?id=$x")
you could use something like
header ("HTTP/1.1 200 OK");
$id = $x;
include ( 'artist.php' );


You can probably do it all in mod_rewrite, but in my experience for something like this, PHP is less frustrating and easier to debug.

<font size=-1>[ This Message was edited by: Brett on 2002-05-16 01:55 ]</font>
Brett
 
Posts: 82
Joined: Tue Jul 10, 2001 4:00 pm
Location: yohost.com

Postby Guest » Wed Jun 12, 2002 10:07 pm

a little trick i used was .
use mod_rewrite to .. take urls like
/home and server side translate them to /index.php?c_method=home
Guest
 

Postby transistor » Thu Jun 13, 2002 7:07 am

Finally I think I have reached a conclusion, I want to share this, so I can get some feedback on what I'm doing.
After lots of reading and writing I think I might have found a solution that might be of interest to others.
I wrote a PHP script that works when an error 404 is found when a user requests, say, http://www.domain.com/somename. (Modify the virtualhost/httpd.conf file)
The script checks for the existance of "somename" (in my database) and if it does, the browser is redirected to http://www.domain.com/artist/x/somename.html (Using PHP's header("location: blabla"); exit; *ALWAYS use exit after a header("location"))
Then a RewriteRule "feeds" the value of "x" to a script in artist to retrieve the info.
rewrite rule ^/(.*)/(.*).html$ /path/to/the/script/index.php?id=$1
That does the trick.
Now further on, I use this property to generate an html file that will provide spiders and robots with links to every artist in my site like so:
<a href="/artist/x/name.html">name</a>
<a href="/artist/x+1/othername.html">other name</a>
etc...
How about that?! :smile:
transistor
 
Posts: 2
Joined: Wed May 01, 2002 4:00 pm
Location: ritmopolis.com

Postby Guest » Fri Jun 21, 2002 4:24 am

Here's an article that attempts to do some similar kinds of things, perhaps with less fuss (and, perhaps no errors?):

http://www.phpbuilder.com/columns/tim20000526.php3
Guest
 

Postby Guest » Fri Jul 05, 2002 8:14 am

I have recently implemented a solution to the problem you outlined above. Most of what you need can be gleened from http://www.alistapart.com/stories/succeed/ - it certainly helped me on the right path...
Guest
 


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 37 guests

cron