我有一个重定向脚本,我有一个快速的问题。
<?php
$user = ;
header ("location: http://socialmedia.com/follow_user=$user");
?>
请注意,socialmedia.com 在服务器上是 ISNT。我想抓住 url 的结尾,所以如果我有一个 index.php 文件,我可以让 example.com/myusername 重定向到 socialmedia.com/follow_user=myusername。
使用parse_url。
例如:
$url = 'http://example.com/myusername';
print_r(parse_url($url));
会给你:
Array
(
[scheme] => http
[host] => example.com
[path] => /myusername
)
因此,您可以采用阵列的该路径部分并根据需要使用它。
<?php
$url = 'http://example.com/myusername'; // the url you start with
$partYouNeed = parse_url($url, PHP_URL_PATH); // get the portion you need
$partYouNeed = ltrim($partYouNeed, '/'); // remove the slash from it
header('location: http://socialmedia.com/follow_user=' . $partYouNeed);