Htaccess重定向到修改后的url


htaccess redirect to modified url

我有多个页面用来为facebook拉元数据,但是它们链接到一个要隐藏的页面。不被看到的url是:

test.local/university/test_name

以上链接应该重定向到:

test.local/content/university

有什么办法我可以做到这与一个重写规则在htaccess?还是需要通过PHP重定向完成?

如果重复,请道歉。

更新:

这就是我解决这个问题的方法。

$domain = $_SERVER["HTTP_HOST"];
$uri = $_SERVER["REQUEST_URI"];
$url = "http://".$domain . $uri;
$parsedURL = parse_url($url);
//    search regex
$regex = "(^'/['d]+-(.*?)/)";
//    get the matched part of the url
if (strpos($_SERVER['HTTP_REFERER'], 'facebook.com') !== false) {
    if (preg_match($regex, $parsedURL['path'], $matches) === 1) {
        $url = $parsedURL['scheme'] . "://" . $parsedURL['host'] . "/content" . $matches[0];
        header('Location: ' . $url);
    }
}

试试这样做…

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^test.local/content/university$    path/to/real/page.php    [NC,L]

您可以在站点根目录的。htaccess中使用此RedirectMatch规则:

RedirectMatch 301 ^/(university)/['w-]+ /content/$1

感谢您的输入。我最后是通过PHP完成的。

$domain = $_SERVER["HTTP_HOST"];
$uri = $_SERVER["REQUEST_URI"];
$url = "http://".$domain . $uri;
$parsedURL = parse_url($url);
//    search regex
$regex = "(^'/['d]+-(.*?)/)";
//    get the matched part of the url
if (strpos($_SERVER['HTTP_REFERER'], 'facebook.com') !== false) {
    if (preg_match($regex, $parsedURL['path'], $matches) === 1) {
        $url = $parsedURL['scheme'] . "://" . $parsedURL['host'] . "/content" . $matches[0];
        header('Location: ' . $url);
    }
}