views:

36

answers:

2

Help please.

I am looking for the best way to replace the "X" (number) in this url

www.websitename.com/info.php?lid=x

the "x" is a numerical value - i would like to replace the "X" with the "name" field from my database.

Is mod rewrite the way to go? I have multiple urls of the same format (different "X" value of course at the end) that i wish to change to create more friendly urls by replacing the "X" with the corresponding value from the database field "name".

If mod rewrite is the way to go can anyone help out with recommended code to go in the htaccess?

Thanks in advance.

+1  A: 

Totally edited: My previous answer was based on a misunderstanding of what you're trying to ask.

What you are asking is to create a friendly URL system. This is covered in many tutorials -- just search for "friendly URLs" and you'll find lots of resources.

Here's a summary of how it works...

To create friendly URLs for your site, you would need something like this in .htaccess (not sure if I got the RewriteRule right because this is completely off the top of my head, so google for a full-blown tutorial to verify):

<IfModule mod_rewrite.c>  
RewriteEngine on
RewriteRule /info/(.+) /info.php?name=$1
</IfModule>

This means a request to http://www.example.com/info/foo would be rewritten to http://www.example.com/info.php?name=foo.

Then you need to modify your application (in particular, the info.php file) to handle this new request format in which the name is given in the URL instead of the id.

Note that in this example, all names (e.g., "foo") must be unique. If any two items in your database have the same name, you're going to have problems. With this in mind, you might want to add a new field to your database table, which is a unique column containing a string using only alphanumeric characters and hyphens appropriate for use in a URL (this type of string is called a slug). You will basically use this slug instead of the id for database queries. Let's say you create an item named "The Discombobulator". When this item is created in your application, it should also create a slug along the lines of "the-discombobulator" and ensure it's unique. If you create a second item also called "The Discombobulator", your app might generate a slug for it like "the-discombobulator-2".

So, when someone requests http://www.example.com/info/the-discombobulator-2, mod_rewrite changes that to http://www.example.com/info.php?name=the-discombobulator-2 and hands it to your app. Your app gets the name parameter, which is "the-discombobulator-2" and looks that up in the database's slug field, and gets the matching record.

Jeff
A: 

I think this is what you are looking for:
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

dave