在灯配置创建个性化网址 [英] Create vanity URLs in a LAMP configuration

查看:138
本文介绍了在灯配置创建个性化网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是LAMP配置下创建用户个性化网址的最佳方式?

What is the best way to create user vanity URLs under a LAMP configuration?

例如,用户的个人资料页可以通过以下方式获得:

For example, a user profile page could be accessed as follows:

http://www.website.com/profile.php?id=1

现在,如果用户输入个性化网址为自己的个人资料我希望的个性化网址加载网页上面。

Now, if a user enters a "vanity URL" for their profile I would want the the vanity URL to load the page above.

例如,如果用户选择i.am.a.user作为自己的个性化网址并在数据库中的用户ID是1,那么的 http://www.website.com/profile.php?id=1 将通过URL访问的的的 http://www.website.com/i.am.a.user

For example, if a user selects "i.am.a.user" as their vanity URL and their user id in the database is 1 then http://www.website.com/profile.php?id=1 would be accessible by that URL and http://www.website.com/i.am.a.user .

我知道在.htaccess但不知道mod-rewrite的,将如何在这里工作。

I'm aware of mod rewrites in .htaccess but not sure how that would work here.

正如我前面提到我的网站是在PHP,MySQL和Linux的和Apache。

As I mentioned my site is in PHP, MySQL, Linux and Apache.

感谢。

推荐答案

说你的其他页面了,你可以对证,下面应该帮助特定的URL。

Say your other pages had specific URLs that you could check against, the following should help.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_]*)$ /profile.php?user=$1 [L]

这有助于保持当前的URL,同时允许用户快捷方式的URL。此外,重写规则将只匹配不包含URL的 / ,这将有助于防止非预期重定向。因此,

This helps to maintain current URLs, while allowing for the user shortcut URLs. Also, the RewriteRule will only match URLs that don't contain a /, which will help protect against non-intended redirects. So,

/i-am-a-user -> MATCHES
/i_am_a_user -> MATCHES
/i-!am-a-user -> NOT MATCHED
/i.am.a.user  -> NOT MATCHED
/i.am.a.user/ -> NOT MATCHED
/some/page/ -> NOT MATCHED
/doesnotexist.php -> NOT MATCHED
/doesnotexist.html -> NOT MATCHED

希望有所帮助。

修改

,这样实际的文件/目录不被重定向,以及确保任何的.php 我已经更新了上述规则。 HTML 文件不会被发送到 profile.php 或者

I've updated the rules above so that actual files/directories aren't redirected as well as making sure that any .php or .html file is not sent to profile.php either.

这篇关于在灯配置创建个性化网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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