如何动态改写Facebook之类的URL [英] How to Dynamically Rewrite a URL like Facebook

查看:100
本文介绍了如何动态改写Facebook之类的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用PHP编写了自己的网站,但不幸的是,Artist个人资料当前如下所示:

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

website.com/profiles.php?id=xxx

website.com/profiles.php?id=xxx

这样,我就可以获取ID并查询数据库.

That way I can GET the id and query the database.

但是,艺术家现在要求提供website.com/artistname作为其URL.我猜想像Facebook和Myspace.

However the Artists are now asking for website.com/artistname as their URL. Like Facebook and Myspace I guess.

尽管如此,我仍然需要获取艺术家ID,以便我可以查询数据库.

I still need to be able to GET the artists id though, so that I can query the database.

我想我将不得不使用mod_rewrite并在某个地方触发php函数,但是我不确定该怎么做!

I guess I'm going to have to use the mod_rewrite and also trigger a php function somewhere but I'm not exactly sure how to do it!

过去的一天,我一直在网上寻找类似的东西,但似乎找不到我想要的东西!

I've spent the past day looking online to find something like this, but I can't seem to find exactly what I want!

如果有人对我该怎么做有任何想法,或者有帮助我的例子,那就太好了!

If anyone's got any ideas on how I could do this, or examples to help me that would be great!

谢谢

推荐答案

如果您不想在URL中使用ID,则艺术家姓名必须是新的ID,因此必须是唯一的.

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

在这种情况下,您可以在文档根目录的.htaccess文件中执行以下操作,以在内部将/<artistname>重写为/profiles.php?name=<artistname>:

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

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

在此,艺术家姓名也被限制为仅由字母组成.对于其他字符(尤其是./),应考虑不允许与现有文件(例如profiles.php)冲突的值.并且,如果您从URL艺术家名称的真实姓名中得出,请确保也不允许存在冲突.在这种情况下,您仍然需要人工ID来使URL路径唯一.

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.

这篇关于如何动态改写Facebook之类的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆