web服务器去掉域名www的301重定向设置方法

avatar 2017年02月09日17:52:58 2 6824 views
博主分享免费Java教学视频,B站账号:Java刘哥
是本文介绍web服务器301跳转方法,实现强制从www.liuyanzhao.com跳到liuyanzhao.com

一、空间的伪静态版本为ISAPI Rewrite 3.1,需要设置301重定向的,只用修改伪静态规则文件( .htaccess )即可。

  1. RewriteEngine on
  2. RewriteCond %{HTTP_HOST} ^www.liuyanzhao.com [NC]
  3. RewriteRule ^(.*)$ http://liuyanzhao.com/$1 [L,R=301]
将以上代码保存到文件名 .htaccess后上传到空间根目录即可。liuyanzhao.com 换成你自己的域名。

二、可用程序实现301重定义,只需修改程序即可。

1、如果是ASP程序,只需在index.asp首页的头部加下以下的绿色代码,liuyanzhao.com换成你自己的域名。
  1. <%
  2. if request.ServerVariables("HTTP_HOST")="www.liuyanzhao.com" then
  3. Response.Status="301 Moved Permanently"
  4. Response.AddHeader "Location","http://liuyanzhao.com"
  5. Response.End
  6. end if
  7. %>
2、如果是PHP程序,只需在index.php首页的头部加下以下的绿色代码,liuyanzhao.com换成你自己的域名。
  1. <?php
  2. if($_SERVER["HTTP_HOST"] == "www.liuyanzhao.com")
  3. {
  4. header("HTTP/1.1 301 Moved Permanently");
  5. header("Location:http://liuyanzhao.com");
  6. }
  7. ?>
3、如果ASP程序,但首页是html静态的实现301
  1. <%
  2. if request.ServerVariables("HTTP_HOST")="www.liuyanzhao.com" then
  3. Response.Status="301 Moved Permanently"
  4. Response.AddHeader "Location","http://liuyanzhao.com"
  5. Response.End
  6. end if
  7. %>
  8. <!--#include file="index.html" -->
把上面代码保存为index.asp(如果文件名有存在可改为 301.asp等)上传到WEB目录下,并在管理平台的“修改默认首页”把index.asp(如改成其他文件名就要填其他文件名301.asp等)调到 最前面。(index.html为你首页的静态文件名) 4、如果PHP程序,但首页是html静态的实现301
  1. <?php
  2. if($_SERVER["HTTP_HOST"] == "www.liuyanzhao.com")
  3. {
  4. header("HTTP/1.1 301 Moved Permanently");
  5. header("Location:http://liuyanzhao.com");
  6. }
  7. ?>
  8. <?php include('index.html'); ?>
把上面代码保存为index.php(如果文件名有存在可改为301.php等)上传到WEB目录下,并在管 理平台的“修改默认首页”把index.php(如改成其他文件名就要填其他文件名301.php等)调到最前面。(index.html为你首页的静态 文件名) 5、如果你是asp.net 设置方法差不多相同,可以去百度搜索下".net 301" 找相关教程。 6、如果你二级栏目或内容页也想实现301,也可以到百度搜索相关的程序教程。 7、http://tool.chinaz.com/pagestatus/ 此工具可以查看设置后有没有生效。 8、app.conf配置
  1. handlers:
  2. - url: /
  3.   script: /index.html
  4. - redirect_host: www.liuyanzhao.com
  5.   status_code: 301
  6.   new_host: liuyanzhao.com
  7. - rewrite_not_exist: (.*)
  8.   script: /index.php/$1
  本文地址:http://liuyanzhao.com/1665.html 转载请注明    
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:1   待审核评论数:0
  1. avatar 久鸽

    为什么会空白页呢,QQ34200148,需要您可以帮助我一下,非常感谢了。