手动键入时,重写URL有效,但不能自动输入

时间:2015-03-18 20:52:03

标签: php .htaccess url rewrite

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

我的网站允许用户输入播放器用户名并显示该用户名的信息。索引页面的URL是

  

http://mcspy.info/

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

  

http://mcspy.info/php.php?username=_scrunch(_scrunch是用户名)。

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

  

http://mcspy.info/player/_scrunch

问题是以上工作正常,但网站不会生成带/ player / Username的URL。它改为使用php.php?=用户名。

如何进行此操作,以便在用户提交用户名时,该网址会自动显示为/player/_scrunch而不是php.php?=_scrunch

这是我的.htaccess文件

RewriteBase /

RewriteEngine On

RewriteRule ^player/(.*)$ php.php?username=$1 [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.



?>

1 个答案:

答案 0 :(得分:1)

尝试在您已有的RewriteRule上方添加此规则:

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

为了将直接请求重定向到php.php文件到更干净的URL。然后你的另一条规则将在内部重写 back 到php.php。