Re: Problem with mod_rewrite and replacing spaces in URL
- From: Jeremy <jeremy@xxxxxxxxxxx>
- Date: Tue, 17 Oct 2006 11:55:44 -0700
David wrote:
Sent this to alt.php a couple of days back, but doesn't look like I'll
get an answer, so trying here.
I'm trying to convert a script to use friendly URLs, I've done this
before, but my PHP skills are quite basic so far, far from proficient
at this.
<snip>
One thing I immediately noticed: your text2url function lowercases everything, but your Britney Spears example is capitalized.
At a more fundamental level, how are you using the URL to lookup the artists themselves? This is where the problem lies. You are going to have to do the same transformation to the artist name when looking up the artist that you did when creating the friendly URL.
For example, here is a simple scheme which lowercases the URL and replaces spaces with dashes, and the corresponding database lookup.
..htaccess:
-----------------------------------------
RewriteRule ^artists/([^/]+) /artists.php?artistName=$1
-----------------------------------------
URL creation:
-------------------------------------------
$url = "/artists/" . strtolower(str_replace(" ", "-", $artistName));
-------------------------------------------
Artist lookup (assumes PDO/PGSQL):
------------------------------------
$artist_query = $pdo->prepare("select * from artists where LOWER(REPLACE(artist_name, ' ', '-')) = :artistName");
$artist_query->bindParam(":artistName", $_GET["artistName"]);
$artist_query->execute();
$artist = $artist_query->fetch();
-----------------------------
This code will obviously not work verbatim; it's just an example of how you need perform the same transformation on artist names when you're looking them up, or the comparison won't work. Notice how in my SQL query I'm comparing the passed-in artist name (in the form of "britney-spears") with the artist name in the database (in the form of "Britney Spears") only AFTER I transform the database version to the proper format - LOWER(REPLACE(...)) transforms "Britney Spears" to "britney-spears", making the comparison successful.
Jeremy
.
- Follow-Ups:
- References:
- Prev by Date: Re: serious problem need help.
- Next by Date: can you set FILES[] variables to session variables?
- Previous by thread: Problem with mod_rewrite and replacing spaces in URL
- Next by thread: Re: Problem with mod_rewrite and replacing spaces in URL
- Index(es):
Relevant Pages
|