将自定义网址链接到没有.htaccess的实际网址

时间:2015-05-04 11:01:38

标签: php url redirect

我有一个要求,我需要创建链接到实际网址的自定义网址。例如,如果确实网址为 www.google.com ,则此网址将显示为 www.exampleurls.com/something/whater 。当我点击此网址时,它将重定向到 www.google.com 。此URL也应该独立,因此href无用。

由于可访问性,我无法配置.htaccess。 :(只有数据库和PHP才有可能吗?

例如:tinyurls,但我不想要小网址。

1 个答案:

答案 0 :(得分:0)

1-选项;

使用任何编码方法对网址进行编码,例如base64base_convert,并将其保存在数据库中的字段中,该字段由您的哈希值CHAR(8)确定。

function foo($url) {
    // yields 7-8 chars, sprintf will fix it to 8 chars padding 0 to the left
    return sprintf("%'08s", base_convert(crc32($url), 16, 36));
    // or use md5 that more reliable for collision issue
    // yields 24-25 chars, sprintf will fix it to 25 chars padding 0 to the left
    // return sprintf("%'025s", base_convert(md5($url), 16, 36));
}

<a href="/redirect/0a5sz8yf">Go to Google!</a>

// redirect file
result = get_url_from_db(hash);
if (result.url == null) {
    header('HTTP/1.1 404 Not Found');
} else {
    header('Location: '. result.url, true, 301);
}

2-选项;

使用正确的URL缩短程序,将短哈希保留在数据库中等等。

相关问题