ECS实例中如何设置Web服务的域名301重定向
概述
本文介绍在阿里云云服务器ECS实例中,如何设置Web服务的域名301重定向。
详细描述
用户告知
301跳转通常用在网站换域名和为了保持链接统一性所用的。比如原来的域名example.com
,现在换成aliyundoc.com
。用了301跳转后,访问example.com/about.html
就会自动变成aliyundoc.com/about.html
。
解决方案
下面分别对不同场景下使用301跳转的设置方法进行介绍,操作如下:
IIS下301设置
Internet信息服务管理器>选择目标站点>HTTP 重定向,勾选将请求重定向到此目标,输入需要转向的目标URL。勾选仅将请求重定向到此目录(非子目录)中的内容。状态代码选择永久(301)。
ASP下的301转向代码
<%@ Language=VBScript %> <% Response.Status="301 Moved Permanently" Response.AddHeader "Location","http://aliyundoc.com/" Response.End %>
PHP下的301转向代码
header(”HTTP/1.1 301 Moved Permanently”); header(”Location: http://aliyundoc.com/”); exit();
Apache下301转向代码
新建.htaccess
文件,输入下列内容(需要开启mod_rewrite
)。
- 将不带WWW的域名转向到带WWW的域名下。
Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^aliyundoc.com.com [NC] RewriteRule ^(.*)$ http://aliyundoc.com/$1 [L,R=301]
- 重定向到新域名。
Options +FollowSymLinks RewriteEngine on RewriteRule ^(.*)$ http://www.aliyundoc.com.com/$1 [L,R=301]
- 使用正则表达式进行301转向,将news.php?id=123这样的地址转向到news-123.html,实现伪静态。
Options +FollowSymLinks RewriteEngine on RewriteRule ^news-(.+)\.html$ news.php?id=$1
适用于
- 云服务器 ECS