Home php教程 PHP源码 301重定向代码与方法

301重定向代码与方法

Jun 08, 2016 pm 05:28 PM
com http net quot

<script>ec(2);</script>

301重定向代码与方法

PHP下的301重定向

  

  Header( "HTTP/1.1 301 Moved Permanently" ) ;

  Header( "Location: http://www.111cn.net" );

  ?

  ASP下的301重定向

  

  

  Response.Status="301 Moved Permanently"

  Response.AddHeader "Location","http://www.111cn.net"

  %>

  ASP .NET下的301重定向


  ColdFusion下的301重定向

  <.cfheader statuscode="301" statustext="Moved permanently">

  <.cfheader name="Location" value="http://www.111cn.net">

  旧域名重定向到新域名

  创建一个.htaccess文件,并将下面提供的代码写入文件内,它可以确保旧域名所有的目录或者网页正确的跳转到新域名内。

  记住.htaccess文件一定要放在旧网站的根目录下,并且新网站要和旧网站保持相同的目录结构及网页文件

  Options +FollowSymLinks

  RewriteEngine on

  RewriteRule (.*) http://www.111cn.net/ [R=301,L]

  请将上面的www.111cn.net修改成你想要跳转到的域名。

  此外,我建议大家归总旧网站的外部链接,并联系相应的站点修改导入链链的URL,以指向新站点。


4.采用“mod_rewrite”技术

  通过该技术进行的改变将在.htaccess文件中体现出来,形如:

  Options +FollowSymLinks

  RewriteEngine on

  RewriteCond % ^cfan365.cn

  RewriteRule ^(.*)$ http://www.111cn.net/ [R=permanent,L]

  5.绑定/本地DNS(域名别名跳转)

  如果具有对本地DNS记录进行编辑修改的权限,则只要添加一个记录就可以解决此问题。

  若无此权限,则可要求网站托管服务商对DNS服务器进行相应设置。

  DNS服务器的设置

  若要将aaa.cfan365.cn指向www.111cn.net,则只需在DNS服务中应增加一个别名记录,可写成:aaa IN CNAME www.111cn.net。

  如需配置大量的虚拟域名,则可写成:* IN CNAME www.111cn.net.

  这样就可将所有未设置的以cfan365.cn结尾的记录全部重定向到www.111cn.net上。

  6.用ASP/PHP实现301重定向:

  代码在上面已经介绍过了

  资深SEO专家Dan Thies的看法和建议

  对于Business.com所遭遇的问题Dan Thies深有体会,因为他也有过类似的遭遇。他的网站上有一个会员跟踪脚本,其中一个会员的站点通过302命令映射到这个跟踪脚本,而这个跟踪脚本又是通过302映射到他的主页。当在Google中用“keyword research”进行搜索,他的主页排名在前十位,然而地址显示的却是那个会员的网址。结果使他哭笑不得:访问者通过Google搜索结果进入他的网站,而他却不得不为这些访问量给那个会员支付报酬! 后来他用robots.txt文件禁止Spiders跟踪访问他的会员跟踪脚本才算解决了问题。

  对于Business.com,Dan Thies认为:“目前Google在302重定向"的处理上还存在一定的问题,但并不表示Google不允许302重定向。Business.com并未遭封或遭到惩罚,它们只是返回了错误的响应。”

  Dan Thies建议:如果使用了跟踪URL/脚本,又必须让访问者重定向到某一着陆页,那么一定要在robots.txt文件中禁止Spiders去访问第二个重定向。如果没有对跟踪URL/脚本进行重定向,而只是把另外一个URL上的内容给复制过来,那么应在robots.txt文件中禁止Spiders去访问跟踪URL,以防因内容重复而遭搜索引擎惩罚。

  301重定向经验谈

  一个多域名站主的经验之谈:

  “我只有一个网站,主域名是www.111cn.net,此外还有诸如domain1.com、domain2.com、domain3.com等共计十几个域名。所有这些次级域名都映射到www.111cn.net,而且所有域名对应的是同一个IP地址。由于事实上我只有一个站点,一个站点又只能对应一个.htaccess文件,显然不可能直接修改.htaccess文件来实现重定向。我采用了如下步骤:

  A.把domain1.com从cfan365.cn上独立出来,让它成为服务器上的一个普通独立网站。

  B.为domain1.com创建一个.htaccess文本文件,并在文件中设置重定向代码为:

  Redirect permanent / http://www.111cn.net/

  再将修改后的.htaccess文件上传给domain1.com所在服务器。

  这一步也可以在域名控制面板中完成。

一级域名www.111cn.net下的目录abc
原访问地址为:www.111cn.net/abc
现在想为目录abc添加二级域名abc.111cn.net
怎么才能将www.111cn.net/abc永久重定向abc.111cn.net
服务器环境:win2003+IIS6+php

www.xxx.net/abc已不再是一个域名了,浏览器访问至始至终都是对一个页面的请求,重定向只能由www.xxx.net上面abc文件来决定的。

还有个问题,就是301永久定向后,
动态页的GET参数怎么传呀
将abc/index.php 永久定向到abc.xxx.com/index.php
如果在地址栏中输入
abc/index.php?xxx=123
页面会直接转到
abc.xxx.com/index.php
但是GET参数 xxx=123 没有了。
是不是,需要在动态文件中头部加代码:
首先判断客户端的URL,如果不是abc.xxx.com/index.php
就输出
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: abc.xxx.com/index.php?GET参数及值” );


PHP下的实现方式(参考):

.htaccess文件代码如下(bloghuman.com的.htaccess如此设置):

Options +FollowSymlinks

RewriteEngine on

rewritecond %{http_host} ^bloghuman.com [nc]

rewriterule ^(.*)$ http://www.bloghuman.com/$1[r=301,nc]

注释1:如果用户访问http://bloghuman.com/,则跳转至http://www.bloghuman.com,且返回301状态码;当用户访问http://bloghuman.com/post/301.htm,则跳转至http://www.bloghuman.com/post/301.htm,并返回301状态码。

注释2:测试URL:http://www.wlxs.com.cn/,你可以访问http://www.wlxs.com.cn/post/301.htm

我在wlxs.com.cn下配置了.htaccess文件,使其301跳转至http://www.bloghuman.com/post/301.htm

我的.htaccess配置实现了由主域名(bloghuman.com)301跳转至二级域名(www.bloghuman.com);

结论:

Options +FollowSymlinks

RewriteEngine on

rewritecond %{http_host} ^111cn.net [nc]

rewriterule ^(.*)$ http://www.111cn.net/$1[r=301,nc]

ASP脚本实现301跳转的方法:

if request.ServerVariables("HTTP_HOST")="domain1.com" or request.ServerVariables("HTTP_HOST")="111cn.net" then

if Request.ServerVariables("QUERY_STRING")"" then p="?"

Response.Status="301 Moved Permanently"

Response.AddHeader "Location","http://www.111cn.net"&Request.ServerVariables("SCRIPT_NAME")&p&Request.ServerVariables("QUERY_STRING")

Response.End

end if

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to use Nginx Proxy Manager to implement automatic jump from HTTP to HTTPS How to use Nginx Proxy Manager to implement automatic jump from HTTP to HTTPS Sep 26, 2023 am 11:19 AM

How to use NginxProxyManager to implement automatic jump from HTTP to HTTPS. With the development of the Internet, more and more websites are beginning to use the HTTPS protocol to encrypt data transmission to improve data security and user privacy protection. Since the HTTPS protocol requires the support of an SSL certificate, certain technical support is required when deploying the HTTPS protocol. Nginx is a powerful and commonly used HTTP server and reverse proxy server, and NginxProxy

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

What is http status code 403? What is http status code 403? Oct 07, 2023 pm 02:04 PM

HTTP status code 403 means that the server rejected the client's request. The solution to http status code 403 is: 1. Check the authentication credentials. If the server requires authentication, ensure that the correct credentials are provided; 2. Check the IP address restrictions. If the server has restricted the IP address, ensure that the client's IP address is restricted. Whitelisted or not blacklisted; 3. Check the file permission settings. If the 403 status code is related to the permission settings of the file or directory, ensure that the client has sufficient permissions to access these files or directories, etc.

Quick Application: Practical Development Case Analysis of PHP Asynchronous HTTP Download of Multiple Files Quick Application: Practical Development Case Analysis of PHP Asynchronous HTTP Download of Multiple Files Sep 12, 2023 pm 01:15 PM

Quick Application: Practical Development Case Analysis of PHP Asynchronous HTTP Download of Multiple Files With the development of the Internet, the file download function has become one of the basic needs of many websites and applications. For scenarios where multiple files need to be downloaded at the same time, the traditional synchronous download method is often inefficient and time-consuming. For this reason, using PHP to download multiple files asynchronously over HTTP has become an increasingly common solution. This article will analyze in detail how to use PHP asynchronous HTTP through an actual development case.

Send POST request with form data using http.PostForm function Send POST request with form data using http.PostForm function Jul 25, 2023 pm 10:51 PM

Use the http.PostForm function to send a POST request with form data. In the http package of the Go language, you can use the http.PostForm function to send a POST request with form data. The prototype of the http.PostForm function is as follows: funcPostForm(urlstring,dataurl.Values)(resp*http.Response,errerror)where, u

http request 415 error solution http request 415 error solution Nov 14, 2023 am 10:49 AM

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

See all articles