How to change URL query string with PHP/.htaccess?

2019-08-24 11:15发布

I want to change URL like below:

http://localhost/register/profile.php?user_id=23

to:

http://localhost/register/username

2条回答
倾城 Initia
2楼-- · 2019-08-24 11:48

You have to enable mod_rewrite. The internet is full of example an tutorials for that.

One example: http://www.sitepoint.com/guide-url-rewriting/

查看更多
在下西门庆
3楼-- · 2019-08-24 12:08

Consider a user of id 23 and username "foo". The easiest is to rewrite /register/23/foo to /register/profile.php?user_id=23 as such:

RewriteEngine on 
RewriteRule ^/register/([0-9]+)/([^/]+)/$ /register/profile.php?user_id=$1 [L,R]

But if you can change profile.php to rely on $_GET['username'] rather than $_GET['user_id'], you can rewrite /register/foo to /register/profile.php?username=foo. Use this rule:

RewriteEngine on 
RewriteRule ^/register/([^/]+)/$ /register/profile.php?username=$1 [L,R]
查看更多
登录 后发表回答