【问题标题】:Username not displaying in URL even when specifying Rewrite rules即使指定重写规则,用户名也不会显示在 URL 中
【发布时间】:2016-02-06 16:52:32
【问题描述】:

我真的很难理解如何使用重写规则修改 URL。我看过以下链接:Directly adding username to URL PHP - 但以下知识似乎对我不起作用。

我需要 url 说例如。 http://localhost/profile_page/Freddy

而不是: http://localhost/profile_page.php

.htaccess(位于默认 Wamp 文件夹中,C:\wamp\www)

    RewriteEngine On
    RewriteBase /wamp/www/

    RewriteRule ^/?$ profile_page.php
    RewriteRule ^/me?$ profile_page.php
    RewriteRule ^/profile_page.php/([a-zA-Z0-9_\-]+)/?$ profile_page.php?u=$1
    RewriteRule ^/profile/([a-zA-Z0-9_\-]+)/?$ profile_page.php?u=$1
    RewriteRule ^/([a-zA-Z0-9_\-]+)/?$ profile_page.php?u=$1

我有以下问题:

1. 有了上面的规则,虽然我在这方面的知识有限,但我希望它能将http://localhost/profile_page.php的url转换为显示登录用户的名称,即http://localhost/profile_page/Freddy .

登录用户的详细信息可以从会话变量$username 或变量$user 中获取,该变量获取url 中“u=”后的用户名(见下文)。但是,该 url 不显示登录用户的名称,它只是显示 http://localhost/profile_page.php,如果以 Freddy 身份登录,我希望它显示 http://localhost/profile_page/Freddy

<?php
    $user = "";
    if (isset($_GET['u'])) {
        $user = ($_GET['u']);
        if (ctype_alnum($user)) { //check if the user exists
            $check = mysqli_query($connect, "SELECT * FROM users WHERE username='$user'");
            if (mysqli_num_rows($check) === 1) {
                $get        = mysqli_fetch_assoc($check);
                $user       = $get['username'];
                $fname      = $get['first_name'];
                echo "<h2>Profile page for: $user</h2>";
            } else { // refresh page 
                echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/index.php\">";
                exit();
            }
        }
    }
?>

2. 如果在 URL 中输入 http://localhost/profile_page.php?u=Fred,它会回显它在 Fred 的页面上,但会显示登录用户的个人资料页面,因此它会显示帖子和信息网址中指定的 Freddy,而不是 Fred。

【问题讨论】:

  • @anubhava - RewriteBase 不正确吗?我知道RewriteBase 为您的重写提供了基础,但假设这意味着根文件夹。
  • @anubhava - 对,我想我现在明白了。谢谢,我已将 RewriteBase 更改为 RewriteBase /。但同样的问题仍然存在,该 URL 不显示用户名或允许我通过在 URL 中绑定他们的用户名来跳转到另一个用户的个人资料页面。

标签: php apache .htaccess url


【解决方案1】:

您可以将 .htaccess 替换为:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^(me)?/?$ profile_page.php [L,NC]

RewriteRule ^profile(?:_page)?/([\w-]+)/?$ profile_page.php?u=$1 [L,QSA,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile_page.php?u=$1 [L,QSA]

【讨论】:

  • 首先感谢您的回复。我已将我的 .htaccess 文件完全更改为您提供的代码。正如我的问题所述,当前的 url(即使使用新的 .htaccess 文件)显示为http://localhost/profile_page.php,当用户第一次点击他们的个人资料按钮(profile_page.php)时,我希望 URL 立即在 url 中打印他们的用户名即http://localhost/profile_page/Freddy.
  • 我还需要/ 之后的内容来定向到用户个人资料。 IE。从 http://localhost/profile_page/Freddy 我可以将 URL 编辑为 http://localhost/profile_page/Alice 并被定向到 Alice 的个人资料页面。或者我需要额外的代码来实现这个功能吗?尽管如此,编辑后的 ​​.htaccess 文件的执行方式与前一个文件相同。
  • 谢谢,我已经开始看到一些使用$user 变量的功能现在显示用户的用户名。但是,当我编辑 URL 并写入时,例如,http://localhost/profile_page/Alice(谁是有效用户) - 它被重定向到的页面是当前登录用户的页面,唯一的区别是 CSS on这个新呈现的页面不存在。除此之外,从http://localhost/profile_page/Alice 转到 home.php(通过单击徽标),然后 URL 更改为 http://localhost/profile_page/home.php 并给我一个 404 Not found 错误。
  • 好消息,页面现在加载了适当的 CSS,非常感谢。坏消息,页面仍在加载相同的内容,即http://localhost/profile_page/alice 显示与http://localhost/profile_page/freddy 相同的页面,例如与 Freddy 相同的个人资料图片等 - 意味着 Alice 的页面,即使 URL 显示它也没有被加载在 Alice 的页面上?
  • 同意。无论如何,感谢您的所有帮助!非常感谢:)
猜你喜欢
  • 2019-09-24
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
相关资源
最近更新 更多