在 LAMP 配置中创建个性化 URL

发布于 2024-11-05 08:43:01 字数 648 浏览 1 评论 0 原文

在 LAMP 配置下创建用户个性化 URL 的最佳方法是什么?

例如,可以按如下方式访问用户配置文件页面:

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

现在,如果用户为其个人资料输入“虚荣 URL”,我希望该虚荣 URL 加载上面的页面。

例如,如果用户选择“i.am.a.user”作为他们的个性 URL,并且他们在数据库中的用户 ID 为 1,则 http://www.website.com/profile.php?id=1 可以通过该 URL 访问 http://www.website.com/i.am.a.user

我知道 .htaccess 中有 mod 重写,但不确定它在这里如何工作。

正如我提到的,我的网站使用 PHP、MySQL、Linux 和 Apache。

谢谢。

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.

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 .

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

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静待花开 2024-11-12 08:43:01

假设您的其他页面有可以检查的特定 URL,以下内容应该会有所帮助。

RewriteEngine on

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

这有助于维护当前 URL,同时允许用户快捷 URL。此外,RewriteRule 将仅匹配不包含 / 的 URL,这将有助于防止非预期重定向。所以,

/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

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]

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

Hope that helps.

EDIT

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.

若言繁花未落 2024-11-12 08:43:01

重写 site.com/user/USERNAME:

在您的根 Web 目录中,放置一个 .htaccess 文件:

RewriteEngine on
RewriteRule ^user/(.+)$ profile.php?name=$1 [L]

这会将所有以“user”开头的请求路由到 profile.php 并将 URI 传递给 $_GET['名称']。如果您有大量文件/目录/其他重写,则首选此方法。

重写 site.com/USERNAME:

RewriteEngine on
# if directory or file exists, ignore
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?name=$1 [L]

仅当请求的文件或目录不存在且请求 URI 不为空(即 www.site.com)时才会路由到 profile.php

< strong>PHP 后端

现在在 profile.php 中,您可以拥有如下内容:

if (!empty($_GET['name'])
    $user = ... // get user by username
else 
    $user = ... // get user by id

Rewrite for site.com/user/USERNAME:

In your root web directory, place a .htaccess file:

RewriteEngine on
RewriteRule ^user/(.+)$ profile.php?name=$1 [L]

This routes all requests that starts with "user" to profile.php and pass the URI to $_GET['name']. This method is preferred if you have a lot of files / directories / other rewrites.

Rewrite for site.com/USERNAME:

RewriteEngine on
# if directory or file exists, ignore
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ profile.php?name=$1 [L]

This routes to profile.php ONLY if the requesting file or directory does not exists, AND when the request URI is not empty (ie, www.site.com)

PHP backend

Now in profile.php, you can have something like this:

if (!empty($_GET['name'])
    $user = ... // get user by username
else 
    $user = ... // get user by id
眉目亦如画i 2024-11-12 08:43:01

首先设置您的 .htaccess 文件,以将对不存在的文件和目录的所有请求发送到单个 php 文件:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) router.php [NC,L]

然后在 router.php 中,查看 $_SERVER['REQUEST_URI'] 以获取您可以使用的用户名在您的查询中获取有关用户的数据。

这假定所有非用户配置文件页面的 URL 作为物理文件存在于您的服务器上。

如果情况并非如此,您可以在 router.php 中执行一些逻辑来决定对每个请求执行哪些操作。在 google 上搜索 php 中的 url 路由,您会得到很多示例。

First setup your .htaccess file to send all requests for files and directories that don't exist to a single php file:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) router.php [NC,L]

Then inside your router.php, look at $_SERVER['REQUEST_URI'] to get the username that you can then use in your query to get the data about the user.

This assumes that all URLs that are not user profile pages exist as physical files on your server.

If that's not the case, you can do some logic in router.php to decide what to do on each request. Do a google search for url routing in php and you'll get plenty of examples.

假装爱人 2024-11-12 08:43:01

好吧,你可以使用 apache RewriteMap 来解决这个问题课程。 RewriteMap 可以是纯文本文件(您根据用户输入的内容定期更新),或者您也可以将其指向脚本(Perl、PHP,任何适合您的文件)来为您进行重写。

有关如何使用 PHP 进行设置的快速摘要,请参阅 通过 PHP 使用 MySQL 数据库控制 mod_rewrite.

Well, you could solve this using an apache RewriteMap as well of course. The RewriteMap can be a plain text file (that you update regularly based on what your users enter), or alternatively you could point it to a script (Perl, PHP, whatever suits you) to do the rewriting for you.

For a quick summary on how to set this up using PHP refer to Using a MySQL database to control mod_rewrite via PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文