简体   繁体   English

PHP从URL删除www

[英]php removing www from url

I am getting an error on a FB app when the url does not have the https or and has a www in the url. 当URL没有https或URL中有www时,我在FB应用程序上遇到错误。 I am just going to strip the url of the www. 我只是要剥离www的网址。

The code below adds the https if it is not in the url but how would I remove the www as well ? 下面的代码添加了https(如果它不在URL中),但是我也将如何删除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");
}
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");
}

easiest way. 最简单的方法。

Try to do it using .htaccess 尝试使用.htaccess

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

Here is a way to do it with your .htaccess with https 这是通过https .htaccess一种方法

RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
  • first line turns your RewriteEngine ... On 第一条线将您RewriteEngine ...... On
  • second line checks to see if https is ... on or 第二行检查https是否在... on
  • third line we check to see if the domain name starts with www 第三行,我们检查域名是否以www开头
  • fourth line if either of the above conditions match rewrite the domain 如果满足上述任一条件,则第四行重写域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM