I've written my site using PHP and unfortunately the Artist profiles are currently like this:
website.com/profiles.php?id=xxx
That way I can GET the id and query the database.
However the Artists are now asking for website.com/artistname as their URL. Like Facebook and Myspace I guess.
I still need to be able to GET the artists id though, so that I can query the database.
I guess I'm going to have to use the mod_rewrite and also trigger a php function somewhere but I'm not exactly sure how to do it!
I've spent the past day looking online to find something like this, but I can't seem to find exactly what I want!
If anyone's got any ideas on how I could do this, or examples to help me that would be great!
Thanks
You can use a URL "slug", whereby a record has a unique name, ie:
then you could perform a query such as:
where slug is the value from the URL.
There are alternatives as I'm sure will be provided.
You can use Gumbo's answer, but you also have one possible shortcut:
You can also redirect the /artistname url to /profiles.php?id=xxx by using a 404 handler
Here's how you would do it:
The good thing about this method, is that you don't have to have mod_rewrite enabled. (if that's a problem)
If you do not want the ID in the URL, the artist names need to be the new ID and thus unique.
In that case you can do the following in the .htaccess file in your document root directory to rewrite
/<artistname>
internally to/profiles.php?name=<artistname>
:Here the artist name is also restricted to only consist of alphabetic letters. For other characters (especially
.
and/
) you should take into account not to allow values that conflict with existing files (e.g.profiles.php
). And if you derive the URL artist names from their real names, make sure not to allow conflicts either. In that case you will still need the artificial ID to make the URL path unique.Firstly, you need alter your artists table, adding a new column for define an alias for artists, like 'david-matthews-band', create an index unique to prevent confusions with homonym artists.
You need edit your profile.php to find artists by alias, like website.com/profile.php?q=david-matthews-band
Create a rewrite rule to transform website.com/profile/david-mattews-band to website.com/profile.php?q=david-matthews-band: