How to Dynamically Rewrite a URL like Facebook

前端 未结 4 407
滥情空心
滥情空心 2021-01-17 03:49

I\'ve written my site using PHP and unfortunately the Artist profiles are currently like this:

website.com/profiles.php?id=xxx

That way I can GET the id and

4条回答
  •  甜味超标
    2021-01-17 04:12

    If you do not want the ID in the URL, the artist names need to be the new ID and thus unique.

    In that case you can do the following in the .htaccess file in your document root directory to rewrite / internally to /profiles.php?name=:

    RewriteEngine on
    RewriteRule ^([a-z]+)$ profiles.php?name=$1
    

    Here the artist name is also restricted to only consist of alphabetic letters. For other characters (especially . and /) you should take into account not to allow values that conflict with existing files (e.g. profiles.php). And if you derive the URL artist names from their real names, make sure not to allow conflicts either. In that case you will still need the artificial ID to make the URL path unique.

提交回复
热议问题