12种实现301网页重定向方法的代码实例(含Web编程语言和Web服务器)
为什么需要使用301重定向:
1. 保留搜索引擎的排名: 301 重定向是最有效的方法,不会影响到搜索引擎对页面的排名。
2. 保留访客和流量: 如果你将页面链接到大量方法可以访问过的地址,如果不是用重定向的话你就会失去这些用户(不解)原文:If you move your popular page to which a lot of visITors have already linked, you may lose them if you don't used redirect method. This PRovides a great way to provide your visitors with the information they were looking for and prevent you From losing your traffic.
下面是 11 中实现 301 重定向的方法:
1. HTML 重定向/Meta 刷新
将下面 HTML 重定向代码放在网页的 head>
节点内:
meta HTTP-EQUIV="REFRESH" content="0;
url=http://www.yourdomain.COM">
上述代码立即将访客重定向到另外一个页面,你可以修改 content 中 的 0 这个值来表示几秒钟后才进行重定向。例如 content="3;
url=http://www.oschina.net/" 表示三秒后再重定向。
2. PHP 重定向
复制代码 代码如下:?
Header( "HTTP/1.1 301 Moved PErmanently" );
Header( "Location: http://www.new-url.com" );
?>
3. ASP Redirect
复制代码 代码如下:
%@ Language=vbScript %>
%
Response.status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>
4. ASP .NET Redirect
复制代码 代码如下:@H_512_60@
script runat="server">
private void Page_Load(object sender, System.Eventargs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
/script>
5. JSP Redirect
复制代码 代码如下:
%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-url.com/" );
response.setHeader( "Connection", "close" );
%>
6. CGI PERL Redirect
复制代码 代码如下:
$q = new CGI;
print $q->
redirect("http://www.new-url.com/");
7. Ruby on Rails Redirect
复制代码 代码如下:
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end
8. Coldfusion Redirect
复制代码 代码如下:
.CFheader statuscode="301" statustext="Moved permanently">
.cfheader name="Location" value="http://www.new-url.com">
9. Javascript URL Redirect
复制代码 代码如下:
head>
script type="text/javascript">
window.location.href='http://www.newdomain.com/';
/script>
/head>
10. IIS Redirect
在 internet 服务管理器中右击你想要重定向的文件和文件夹,选择 "a redirection to a URL".
然后输入目标网址,选中 "The exact url entered above" 和 "A permanent redirection for this resource" 然后点击 'Apply' 按钮。
11. 使用 .htaccess 进行重定向
创建一个 .htaccess 文件(代码如下)用来将访问呢 domain.com 重定向到 www.domain.com 下,该文件必须放置在网站的root目录,也就是首页放置的目录。
复制代码 代码如下:Options +FollowSymlinks
RewriteEngine on
RewriteCond %{
http_host}
^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
注意: .htaccess 方法的重定向只能在 Linux 下使用 apache 的 mod_rewrite 模块启用的情况下使用。
12.Nginx中的rewrite
复制代码 代码如下:
server {
server_name www.js-code.com jb51.net;
if ($host = 'jb51.net' ) {
rewrite ^/(.*)$ https://www.js-code.com/$1 permanent;
}
- Python语言的面相对象编程方式初步学习
- 解析C语言基于UDP协议进行Socket编程的要点
- 浅谈C语言编程中程序的一些基本的编写优化技巧
- Linux下C语言实现C/S模式编程
- Go语言编程入门超级指南
- C语言编程中的联合体union入门学习教程
- C语言编程入门之程序头文件的简要解析
- 12种最常用的网页编程语言简介(值得收藏)
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: 12种实现301网页重定向方法的代码实例(含Web编程语言和Web服务器)
本文地址: https://pptw.com/jishu/608554.html