7

I am a pretty skilled programmer, but when it comes to RegEx and rewriting, I am a total n00b. I want to convert a URL from

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

to

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

where "item" refers to the name of an item in a database that is being looked-up.

I'm using LAMP (Linux, Apache, MySQL, PHP) and I cannot, for the life of me, figure out how to convert the URLs so they are SEO friendly.

2
  • 1
    Looks like u fear them.Please grab a copy of Mastering Regular Expressions. Commented Nov 29, 2010 at 7:01
  • I would also recommend Regular Expressions Cookbook. Commented May 4, 2016 at 19:53

3 Answers 3

4

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 will recognise dashes) string of any length as the 'id'. You can limit this to just numeric by changing the regex 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 one will match /lookup/123/some-text/ to /lookup.php?id=123&view=some-text

Sign up to request clarification or add additional context in comments.

Comments

2

Take a look on htaccess rewrite urls! :)

Here's your example:

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

When you access lookup/123/my-product/, it'll call the lookup.php?id=123&name=my-product file internally.

2 Comments

Now the problem is that other pages that follow the format don't work. I want to get another page example.com/category.php?id=1 to appear as example.com/category/1/name
What if the website is in subfolder?
0

Do you want to redirect from the old URL to the new? Or are you looking to read in those "friendly" URLs and then have them behave like the old URL?

If it's the latter, try taking a look at Net_URL_Mapper as a way to parse and redirect those links fairly easily.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.