Rewrite URL 在手动输入时有效,但不能自动输入

Rewrite URL works when typed manually, but not automatically

好的,问题的标题可能看起来含糊不清,所以这里有一个完整的解释。

我的站点允许用户输入玩家用户名并显示该用户名的信息。索引页的 URL 是

http://mcspy.info/

用户提交用户名后,他们将被带到显示结果的另一个页面。 URL 现在看起来像这样

http://mcspy.info/php.php?username=_scrunch (_scrunch being a username).

现在通过使用 .htaccess 文件重写 URL,URL 现在看起来像这样

http://mcspy.info/player/_scrunch

问题是上面的工作正常,但站点不会生成 URL 和 /player/Username。它改为使用 php.php?=username.

我该怎么做才能让当用户提交用户名时,URL 会自动显示为 /player/_scrunch 而不是 php.php?=_scrunch

这是我的 .htaccess 文件

RewriteBase /

RewriteEngine On

RewriteRule ^player/(.*)$ php.php?username= [L]

这是我的 php.php 文件(只是 PHP 代码)。

<?php
//error_reporting(E_ALL & ~E_NOTICE);
// Load the username from somewhere
if (
$username = $_GET["username"]
) {
//do nothing 
} else {
$username = "notch";
}


//allow the user to change the skin
$skinChange = "<a href='https://minecraft.net/profile/skin/remote?url=http://skins.minecraft.net/MinecraftSkins/$username.png' target='_blank' </a>";

//grabbing the users information
if ($content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username))
) {
$userSkin3D = "<img src='https://mcapi.ca/skin/3d/$username' />";
$userSkin2D = "<img src='https://mcapi.ca/skin/2d/$username' />";
} else {
$content = file_get_contents('https://api.mojang.com/users/profiles/minecraft/' . urlencode($username) . '?at=0');
if( $http_response_header['0'] == "HTTP/1.1 204 No Content") {
header ('Location: http://mcspy.info/php.php?username=notch');
}
$json = json_decode($content);

foreach ($json as $currentName) {
$currentName = $currentName;
}

$userSkin3D = "<img src='https://mcapi.ca/skin/3d/$currentName' />";
$userSkin2D = "<img src='https://mcapi.ca/skin/2d/$currentName' />";
}



// Decode it
$json = json_decode($content);

// Check for error
if (!empty($json->error)) {
die('An error happened: ' . $json->errorMessage);
}

// Save the uuid
$uuid = $json->id;

// Get the history (using $json->uuid)
$content = file_get_contents('https://api.mojang.com/user/profiles/' . urlencode($uuid) . '/names');

// Decode it
$json = json_decode($content);

$names = array(); // Create a new array

foreach ($json as $name) {
$input = $name->name;

if (!empty($name->changedToAt)) {
    // Convert to YYYY-MM-DD HH:MM:SS format
    $time = date('Y-m-d H:i:s', $name->changedToAt);

   // $input .= ' (changed at ' . $time . ')';
}

$names[] = $input; // Add each "name" value to our array "names"

}

//url to users 2D head (avatar)
$usersAvatar = "https://mcapi.ca/avatar/2d/$input/55";

//user's Avatar as favivon
$usersFavicon = "<link rel='shortcut icon' href='$usersAvatar' type='image/png' />";
//use $uuid tp grab UUID of the user - ---- - - - use $names to get name history of the user.



?>

尝试将此规则添加到您已有的 RewriteRule 之上:

RewriteCond %{THE_REQUEST} \ /+php\.php\?username=([^&\ ]+)
RewriteRule ^ /player/%1? [L,R]

为了将对 php.php 文件的直接请求重定向到看起来更干净的 URL。然后您的其他规则将在内部将其 改写 为 php.php 。