Using PHP how do I make a rewrite rule? [duplicate

2019-02-20 22:05发布

This question already has an answer here:

Using mod rewrite for the first time. Please help me with these rules

I'd like my urls rewritten for pages as follows:

list.php?city=dallas >>> list/city/dallas

profile.php?id=12 >>> profile/zaknuman (username retrieved from db)

story.php?id=33 >>> story/there-are-no-ants-in-texas (story title retrieved from db)

1条回答
Explosion°爆炸
2楼-- · 2019-02-20 22:28
RewriteRule list/city/([a-zA-Z])$ list.php?city=$1

which should match every character in the range a-z and A-Z after the final slash.

The other two I believe you'll need to embed the slug ('zaknuman' and 'there-are-no-ants-in-texas') in the database and then you'll be able to retrieve that slug from the database and get your ID that way, vis:

RewriteRule profile/(.*)$ profile.php?slug=$1

RewriteRule story/(.*)$ story.php?slug=$1

These last 2 match every character after the final slash.

EDIT: Don't forget to make sure you have "RewriteEngine On" in your .htaccess file or Apache configuration!

查看更多
登录 后发表回答