使用PHP将Hervo上的非www重定向到www

时间:2012-12-05 20:43:49

标签: php redirect heroku dns cname

我在Heroku上有一个简单的php应用程序,根据他们的推荐,我的自定义域名的DNS指向“www.biglikeco.com”。输入“biglikeco.com”并不是指向任何地方。

在Heroku的php应用程序环境中,将所有流向“biglikeco.com”的流量重定向到“www.biglikeco.com”的最佳方法是什么?

谢谢。

1 个答案:

答案 0 :(得分:2)

这是我为您创建的通用PHP重定向脚本,您可以将其附加到任何可以执行所需操作的全局包含文件:

$host = $_SERVER['HTTP_HOST'];
if (!preg_match('/^www\..*/', $host)) {
    header('location: http://www.my_site.com');
}

或者使用.htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^my_site\.com [NC]
RewriteRule (.*) http://www.my_site.com/$1 [L,QSA,R=301]
相关问题