SilverStripe。如何从www.domain名称重定向到域名?

时间:2014-09-24 15:56:01

标签: redirect silverstripe

我希望找到方法的反面:

Director::forceWWW();

我的主要域名应该是 domainName.com没有www。

2 个答案:

答案 0 :(得分:2)

没有SilverStripe功能可将所有www链接重定向到非www链接。

相反,您可以编写.htaccess RewriteRule来执行此操作。

在您的网站根.htaccess文件中,在现有RewriteEngine On行下添加以下代码:

...

<IfModule mod_rewrite.c>
    SetEnv HTTP_MOD_REWRITE On
    RewriteEngine On

    ### Redirect www links to non www links ###

    RewriteCond %{HTTP_HOST} ^www\.domainName\.com$ [NC]
    RewriteRule (.*) http://domainName.com/$1 [R=301,L]

    ...

答案 1 :(得分:1)

3dgoo的解决方案更快,因为它不涉及任何PHP,但我做了......

class PreventWWW extends DataExtension {
    public static function stopWWW() {
        if(!(strpos($_SERVER['HTTP_HOST'], 'www') !== 0)) {
            $destURL = str_replace(Director::protocol() .'www.', Director::protocol() , 
                Director::absoluteURL($_SERVER['REQUEST_URI']));
            header("Location: $destURL", true, 301);
            die("<h1>Your browser is not accepting header redirects</h1>"
                . "<p>Please <a href=\"$destURL\">click here</a>");
        }
    }
}

在yml config中:

Director:
  extensions:
    - PreventWWW

因为我只是在live-env上设置它,所以我在_config.php

中有这个
PreventWWW::stopWWW();

还应该可以在yml per“Only”中设置它依赖于环境(live / dev / stage)。看这里.. http://doc.silverstripe.org/framework/en/topics/configuration