【问题标题】:php removing www from urlphp从url中删除www
【发布时间】:2015-11-30 18:32:46
【问题描述】:

当 url 没有 https 或在 url 中有 www 时,我在 FB 应用程序上遇到错误。我只是要剥离 www 的 url。

如果 https 不在 url 中,下面的代码会添加 https,但 我将如何删除 www 以及

if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
    $redirect = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: $redirect");
}

【问题讨论】:

    标签: php url https


    【解决方案1】:
    if(!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == ""){
        $redirect = str_replace('www.', '', "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: $redirect");
    }
    

    最简单的方法。

    【讨论】:

    • 最后缺少一个 )。我修好了它,它可以工作了。
    • 为后代更新
    【解决方案2】:

    尝试使用 .htaccess 来实现

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

    【讨论】:

    • 呃,他也在找...https
    【解决方案3】:

    这是一种使用.htaccesshttps 的方法

    RewriteEngine On
    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    • 第一行变成你的RewriteEngine ... On
    • 第二行检查 https 是否为 ... on
    • 第三行我们检查域名是否以www开头
    • 第四行,如果上述任一条件匹配,则重写域

    【讨论】:

      猜你喜欢
      • 2018-09-18
      • 2014-12-09
      • 2011-09-14
      • 2016-10-01
      • 2013-12-02
      • 1970-01-01
      • 2017-03-31
      • 2011-09-24
      • 1970-01-01
      相关资源
      最近更新 更多