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

感情迁移 提交于 2020-01-06 07:51:05

问题


I want to change URL like below:

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

to:

http://localhost/register/username

回答1:


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]



回答2:


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/



来源:https://stackoverflow.com/questions/9528728/how-to-change-url-query-string-with-php-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!