Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

apache - How do I convert a PHP query string into a slash-based URL?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...