银条.如何从 www.domainName 重定向到 domainName? [英] SilverStripe. How to redirect from www.domainName to domainName?

查看:22
本文介绍了银条.如何从 www.domainName 重定向到 domainName?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找相反的方法:

I'm looking to find the reverse of the method:

导演::forceWWW();

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

My main domain should be domainName.com without www.

推荐答案

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

3dgoo's solution is faster since it does not involve any php but I did...

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 配置中:

Director:
  extensions:
    - PreventWWW

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

since I set it just on live-env I had this in _config.php

PreventWWW::stopWWW();

也应该可以根据仅"在 yml 中的环境(live/dev/stage)设置它.看这里..http://doc.silverstripe.org/framework/en/topics/configuration

It should also be possible to set it dependent on environment (live/dev/stage) in yml per "Only". See here.. http://doc.silverstripe.org/framework/en/topics/configuration

这篇关于银条.如何从 www.domainName 重定向到 domainName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆