2015-03-18 21 views
0

好的,所以问题的标题可能看起来很模糊,所以这里有一个完整的解释。重写URL在手动输入时有效,但不会自动输入

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

http://mcspy.info/

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

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

使用.htaccess文件重写URL,一个URL可以现在这个样子

http://mcspy.info/player/_scrunch

的问题是,上述工作正常,但网站不会使用/ player/Username生成网址。它改为使用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. 



?> 
+0

如果这是一个带有'method =“get”'的表单,表单将始终将用户发送到通过表单字段(如'key = value')指定的页面。您需要在javascript中更改表单的操作以更改该操作,可能需要提交或使提交按钮成为标准按钮,该按钮只是调用更改操作的js脚本。除此之外,您还可以检查'php.php'中的$ _SERVER ['REQUEST_URI']',如果它不是预期的格式,请重定向用户。 – 2015-03-18 21:00:01

+0

嗯..好吧,我需要以任何方式改变我的PHP代码?由于我不是最大的JavaScript,有没有任何教程来告诉我如何做到这一点?甚至是一些示例代码? – JLWillliamsUK 2015-03-18 21:03:25

+0

首先你需要回答一个问题:为什么你要使用重写?如果你的表单以这种方式发送并且它有效,那么你很好。但是,如果要添加锚点,则可以使用重写格式。 – 2015-03-18 21:04:59

回答

1

尝试添加你已经有RewriteRule上面这条规则:

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

,以请求定向到php.php文件重定向到清洁寻找URL。然后你的其他规则将在内部重写它到php.php之一。

+0

Yeap。这很好用!非常感谢! – JLWillliamsUK 2015-03-18 21:25:11