JLWillliamsUK
JLWillliamsUK

Reputation: 185

Rewrite URL works when typed manually, but not automatically

Okay so the title of the question may seem vague so here's a full explanation.

My site allows a user to enter a player Username and have the information of that username displayed. the URL of the index page is

http://mcspy.info/

Once the user submits the username, they are taken to another page where it displays the result. The URL now looks like this

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

Now by using a .htaccess file to rewrite the URL, a URL can now look like this

http://mcspy.info/player/_scrunch

The Problem is that the above works fine, but the site doesn't generate the URL with /player/Username. It instead uses the php.php?=username.

How would I go about doing it so that when the user submits a username, the URL will automatically show like /player/_scrunch instead of php.php?=_scrunch

Here's my .htaccess file

RewriteBase /

RewriteEngine On

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

Here's my php.php file (Just the PHP code).

<?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.



?>

Upvotes: 0

Views: 85

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Try adding this rule above the RewriteRule that you already have:

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

in order to redirect direct requests to the php.php file to the cleaner looking URL. Then your other rule will internally rewrite it back to the php.php one.

Upvotes: 1

Related Questions