不带www域名跳转到www

      发布在:后端技术      评论:0 条评论

网站域名为了统一性,一般会要求不带www的域名,比如 80zx.com 访问时跳转到 www.80zx.com 这样带www开头的域名

nginx网站环境伪静态的方式解决方法

if ($host ~ '^xxxxx.com'){
    return 301 http://www.xxxxx.com$request_uri;
}

image

Apache网站环境伪静态的解决方法

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

IIS10网站环境规则如下

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^80zx\.com$" />
          </conditions>
          <action type="Redirect" url="http://www.80zx.com/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

PHP程序代码的解决方法

  $host = strtolower($_SERVER['HTTP_HOST']);
    if ($host == 'xxxxx.com') {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: https://www.xxxxx.com");exit;
    }


相关文章
热门推荐