Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 即使在指定重写规则时,用户名也不显示在URL中_Php_Apache_.htaccess_Url - Fatal编程技术网

Php 即使在指定重写规则时,用户名也不显示在URL中

Php 即使在指定重写规则时,用户名也不显示在URL中,php,apache,.htaccess,url,Php,Apache,.htaccess,Url,我真的很难理解如何使用重写规则修改URL。我看到了以下链接:-但以下知识似乎不适合我 我需要的网址说,例如。 http://localhost/profile_page/Freddy 而不是: http://localhost/profile_page.php .htaccess(位于默认Wamp文件夹C:\Wamp\www中) 我有以下问题: 1。根据上述规则,尽管我在该领域的知识有限,但我希望它能转换http://localhost/profile_page.php显示登录用户的姓名,即ht

我真的很难理解如何使用重写规则修改URL。我看到了以下链接:-但以下知识似乎不适合我

我需要的网址说,例如。
http://localhost/profile_page/Freddy

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

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

我有以下问题:

1。根据上述规则,尽管我在该领域的知识有限,但我希望它能转换
http://localhost/profile_page.php
显示登录用户的姓名,即
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();
            }
        }
    }
?>

您可以将.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替换为以下内容:

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]

@anubhava-重写库是否不正确?我知道,
RewriteBase
为您的重写提供了一个基础,但假设这意味着根文件夹。@anubhava-对,我想我现在知道了。谢谢,我已将RewriteBase更改为
RewriteBase/
。但同样的问题仍然存在,URL没有显示用户名,也不允许我通过在URL中绑定用户名跳转到另一个用户配置文件页面。@anubhava-重写库是否不正确?我知道,
RewriteBase
为您的重写提供了一个基础,但假设这意味着根文件夹。@anubhava-对,我想我现在知道了。谢谢,我已将RewriteBase更改为
RewriteBase/
。但同样的问题依然存在,URL没有显示用户名,也不允许我通过在URL中绑定用户名跳转到另一个用户配置文件页面。首先,感谢您的回复。我已将
.htaccess
文件完全更改为您提供的代码。如我在问题中所述,当前的url(即使使用新的.htaccess文件)显示为
http://localhost/profile_page.php
,当用户第一次单击其配置文件按钮(profile\u page.php)时我希望URL能立即在URL中打印他们的用户名,即
http://localhost/profile_page/Freddy
。我还需要
/
后面的内容来指向用户配置文件。即从
http://localhost/profile_page/Freddy
我可以将URL编辑为,即
http://localhost/profile_page/Alice
并直接转到Alice的个人资料页面。或者我是否需要此功能的附加代码?尽管如此,已编辑的.htaccess文件的执行方式与前一个文件相同。谢谢,我已经开始看到使用
$user
变量的两个功能现在显示用户的用户名。但是,当我编辑URL并编写时,例如,
http://localhost/profile_page/Alice
(谁是有效用户)-重定向到的页面是当前登录用户的页面,唯一的区别是新呈现页面上的CSS不存在。除此之外,从
http://localhost/profile_page/Alice
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的页面上,也不会加载?同意。无论如何,谢谢你的帮助!非常感谢:)首先,谢谢你的回复。我已将
.htaccess
文件完全更改为您提供的代码。如我在问题中所述,当前的url(即使使用新的.htaccess文件)显示为
http://localhost/profile_page.php
,当用户第一次单击其配置文件按钮(profile\u page.php)时我希望URL能立即在URL中打印他们的用户名,即
http://localhost/profile_page/Freddy
。我还需要
/
后面的内容来指向用户配置文件。即从
http://localhost/profile_page/Freddy
我可以将URL编辑为,即
http://localhost/profile_page/Alice
并直接转到Alice的个人资料页面。或者我是否需要此功能的附加代码?尽管如此,已编辑的.htaccess文件的执行方式与前一个文件相同。谢谢,我已经开始看到使用
$user
变量的两个功能现在显示用户的用户名。但是,当我编辑URL并编写时,例如,
http://localhost/profile_page/Alice
(谁是有效用户)-重定向到的页面是当前登录用户的页面,唯一的区别是新呈现页面上的CSS不存在。除此之外,从
http://localhost/profile_page/Alice
URL随后更改为
http://localhost/profile_page/home.php
并给我一个404 Not found错误。好消息,页面现在加载了相应的CSS,非常感谢。坏消息是,页面仍在加载相同的内容,即
http://localhost/profile_page/alice
显示与
http://localhost/profile_page/freddy
例如与Fre相同的配置文件图片