How to convert a PHP query string to a slash?

I'm a pretty experienced programmer, but when it comes to RegEx and rewriting, I'm full n00b. I want to convert URLs from

http://www.example.com/lookup.php?id=1 

to

 http://www.example.com/lookup/1/item/ 

where "item" refers to the name of the item in the database being viewed.

I use LAMP (Linux, Apache, MySQL, PHP), and I cannot, for life, figure out how to convert URLs to be SEO friendly.

+7
php apache mod-rewrite
source share
3 answers

A simple .htaccess example:

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^lookup/([a-z0-9\-]+)/item/?$ /lookup.php?id=$1 </IfModule> 

This will match any alphanumeric (also recognizing dash) string of any length as "id". You can limit this to only numeric by changing the regular expression to ([0-9]+) .

 <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^lookup/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /lookup.php?id=$1&view=$2 </IfModule> 

This character will match from /lookup/123/some-text/ to /lookup.php?id=123&view=some-text

+4
source share

Look at htaccess rewrite url! :)

Here is your example:

 RewriteRule ^lookup/(\d+)/(.*)$ /lookup.php?id=$1&name=$2 

When accessing lookup/123/my-product/ it will internally call the file lookup.php?id=123&name=my-product .

+1
source share

Do you want to redirect from the old URL to the new? Or do you want to read in these "friendly" URLs and then act like the old URL?

If this is the last one, try looking at Net_URL_Mapper as a way to parse and redirect those links quite easily.

0
source share

All Articles