Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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 使用HTACCESS根据变量重定向到url_Php_.htaccess_Redirect_Social Networking - Fatal编程技术网

Php 使用HTACCESS根据变量重定向到url

Php 使用HTACCESS根据变量重定向到url,php,.htaccess,redirect,social-networking,Php,.htaccess,Redirect,Social Networking,我想做一个服务,允许用户获得一个直接的链接来做一个动作,如跟随或订阅(没有明显的原因告诉) 因此,使用htaccess或php(无论什么更好),我如何做以下工作 example.com/insertUserName此处自动重定向到somepopularsocialmediasite.com/?follow_user=blahblahblah 需要注意的是,example.com站点与somepopularsocialmediasite.com位于不同的服务器上 另外,我希望有一些.html页面,

我想做一个服务,允许用户获得一个直接的链接来做一个动作,如跟随或订阅(没有明显的原因告诉)

因此,使用htaccess或php(无论什么更好),我如何做以下工作 example.com/insertUserName此处自动重定向到somepopularsocialmediasite.com/?follow_user=blahblahblah

需要注意的是,example.com站点与somepopularsocialmediasite.com位于不同的服务器上


另外,我希望有一些.html页面,包括index.html、about.html等,因此我需要一种方法来排除重定向中的某些查询/文件。

我假设您可以以某种方式将用户名存储在一个变量中,因此我们可以这样做:

$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$username = 'some_username_you_get_somehow';
$url_username = substr(strrchr($current_url, '/'), 1);

if ($url_username == $username)
{
    header("Location: http://somepopularsocialmediasite.com/?follow_user={$username}/");
}

如果服务器上存在的所有页面(
index.html
等)都具有
.html
扩展名,您只需执行以下操作:

RewriteRule ^([a-zA-Z0-9]+)$ http://socialsite.com/?follow_user=$1 [R=301, L]

基本上,该规则不会匹配任何包含扩展名的内容,因为它只查找不区分大小写的字母和数字。如果您愿意,您甚至可以在其中添加连字符。

谢谢您的回复。然而,现在我想起来了,我必须使用htaccess来实现我想要实现的目标。我想这样做,假设某人有社交媒体档案。他们可以直接转到example.com/profilename,它会将他们重定向到socialmediasite.com/?follow\u user=profilenameSorry,但我无法进一步帮助您,因为我不太了解.htaccess语法。