search
Homephp教程php手册apache+php完美解决301重定向的两种方法
apache+php完美解决301重定向的两种方法Jun 13, 2016 pm 12:08 PM
301PerfectmethodhaveefficientofablesolveRedirect

幸好有301重定向能有效解决这样的问题。正如月光博客这篇文章中说的,
301重定向可促进搜索引擎优化效果
从搜索引擎优化角度出发,301重定向是网址重定向最为可行的一种办法。当网站的域名发生变更后,搜索引擎只对新网址进行索引,同时又会把旧地址下原有的外部链接如数转移到新地址下,从而不会让网站的排名因为网址变更而收到丝毫影响。同样,在使用301永久性重定向命令让多个域名指向网站主域时,亦不会对网站的排名产生任何负面影响。

关于301重定向的更多内容,大家不妨Google一下。本文只介绍实现方法啦!
301重定向的实现,我以前也写过一篇相关的文章,但这篇文章的解决方法比较简单,只能实现主页的跳转,本文介绍的这两种方法,可以完美实现301重定向。

方法1:修改.htaccess文件
代码如下:

复制代码 代码如下:



RewriteEngine On
RewriteCond %{HTTP_HOST} blog.iflyhigher.tk$ [NC]
RewriteRule ^(.*)$ http://blog.jb51.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} iflyhigher.tk$ [NC]
RewriteRule ^(.*)$ http://jb51.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} moiya.tk$ [NC]
RewriteRule ^(.*)$ http://jb51.net/$1 [R=301,L]


本博需要对三个域名进行重定向,所以写的比较多,关键代码就是2句话

复制代码 代码如下:


RewriteCond %{HTTP_HOST} blog.iflyhigher.tk$ [NC]
RewriteRule ^(.*)$ http://blog.jb51.net/$1 [R=301,L]


红色的域名是需要被重定向的旧域名,绿色的是现在网站的域名。
方法2:使用PHP的重定向代码
新建一个index.php文件,然后参考下面代码按自己的重定向要求做简单修改:

复制代码 代码如下:


$the_host = $_SERVER['HTTP_HOST'];
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
switch ($the_host)
{
case "www.iflyhigher.tk":
case "iflyhigher.tk":
$location = "Location: http://jb51.net" . $request_uri;
break;
case "blog.iflyhigher.tk":
$location = "Location: http://blog.jb51.net" . $request_uri;
break;
case "www.moiya.tk":
case "moiya.tk":
$location = "Location: http://jb51.net";
break;
default:
$location = "Location: http://jb51.net";
break;
}
header('HTTP/1.1 301 Moved Permanently');
header($location);
exit();
?>


如果只要对一个域名进行重定向,可以把代码简化成下面的形式:

复制代码 代码如下:


$the_host = $_SERVER['HTTP_HOST'];//取得进入所输入的域名
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//判断后面的请求部分
if($the_host !== 'jb51.net')//jb51.net是我现在的域名
{
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location: http://jb51.net'.$request_uri);//跳转到我的新域名地址
exit();
}
?>


注意,最后的exit()函数是一定要写的,我最初就没有写,结果只能重定向首页,像http://blog.iflyhigher.tk/guestbook这样的网页,就无法进行重定向。
最后,关于重定向的一些细节
由于要对三个域名进行重定向,重定向前,我首先将这三个域名作为Addon Domain绑定到我的服务器上去,并让这三个域名指向同一个文件夹,这样,只要修改这一个文件夹中的.htaccess文件或者index.php文件就可以了。如果没有.htaccess文件或者index.php文件,新建一个即可。
希望本文对需要进行301重定向的朋友有帮助。
转载请表明出处: Gevin的博客
Statement
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
golang 报错:“undeclared name…” 如何解决?golang 报错:“undeclared name…” 如何解决?Jun 24, 2023 pm 03:31 PM

Golang(Go编程语言)是一种基于C语言的编程语言,被广泛用于Web开发、网络编程、操作系统等领域。然而,在编写Golang程序时经常会遇到一个常见的问题,就是“undeclaredname”(未声明名称)错误。下面将介绍如何解决这个问题。了解错误信息在编译和运行Golang程序时,如果遇到了未声明名称错误,会在控制台输出相应的错误信

Java中的ClassNotFoundException——找不到类要怎么解决?Java中的ClassNotFoundException——找不到类要怎么解决?Jun 25, 2023 am 08:30 AM

Java中的ClassNotFoundException是一种常见的编译错误。当我们尝试使用Java虚拟机(JVM)加载某个类时,如果JVM找不到该类,就会抛出ClassNotFoundException。这个错误可能出现在程序运行时,也可能出现在编译时。在本文中,我们将讨论什么是ClassNotFoundException,它为什么会发生以及如何解决它。C

golang 编译错误:"undefined: json.Marshal" 如何解决?golang 编译错误:"undefined: json.Marshal" 如何解决?Jun 24, 2023 pm 03:24 PM

Go语言是一门越来越受欢迎的编程语言,它的简洁、高效、易于编写的特点已经被越来越多的开发者所认可。而在Go语言开发中,遇到编译错误是不可避免的。其中一个常见的错误就是“undefined:json.Marshal”。这个错误通常发生在你使用了Go标准库的“encoding/json”包时,编译器提示找不到“json.Marshal”的定义。这个问题的根本原

Java错误:JDBC错误,如何解决和避免Java错误:JDBC错误,如何解决和避免Jun 24, 2023 pm 02:40 PM

随着Java的广泛应用,Java程序在连接数据库时经常会出现JDBC错误。JDBC(JavaDatabaseConnectivity)是Java中用于连接数据库的编程接口,因此,JDBC错误是在Java程序与数据库交互时遇到的一种错误。下面将介绍一些最常见的JDBC错误及如何解决和避免它们。ClassNotFoundException这是最常见的JDBC

golang 报错:“undefined variable or function” 如何解决?golang 报错:“undefined variable or function” 如何解决?Jun 24, 2023 pm 05:18 PM

Go语言作为一门快速发展的编程语言,被广泛应用于各种项目和领域。然而,在使用golang编写程序时,你有可能会遇到一些报错,其中一个常见的报错是“undefinedvariableorfunction”。那么,这个错误是什么意思?它是如何产生的?又该如何解决呢?本文将会对这些问题进行探讨。首先,我们需要了解一些基本概念。在golang中,变量和函数是两

golang 报错:“missing return…” 如何解决?golang 报错:“missing return…” 如何解决?Jun 24, 2023 pm 04:37 PM

在golang中,函数在定义时需要明确返回值类型和返回值,但有时候会出现“missingreturn…”的错误,表示函数缺少返回语句。本文将介绍如何解决这个问题。确认函数签名如果在定义函数时声明了返回值类型,但没有返回具体的值,就会触发这个错误。因此,你可以先检查函数签名,确认声明了返回值类型。举个例子:funcadd(a,bint)int{

Java错误:Gradle错误,如何解决和避免Java错误:Gradle错误,如何解决和避免Jun 25, 2023 am 11:13 AM

Java作为目前最受欢迎的编程语言之一,在开发过程中常常会遇到各种各样的错误,其中Gradle错误是比较常见的一种。本文将介绍如何解决和避免Gradle错误。一、Gradle错误的原因Gradle为构建工具,其主要作用是将代码、资源文件、第三方库等打包并生成可执行的应用程序。在实际开发中,如果不注意一些细节,很容易出现Gradle错误,主要原因通常有以下几种

在Vue应用中遇到“SyntaxError: Unexpected token”怎么解决?在Vue应用中遇到“SyntaxError: Unexpected token”怎么解决?Jun 24, 2023 pm 06:55 PM

在Vue应用中遇到“SyntaxError:Unexpectedtoken”怎么解决?Vue是前端开发中广泛使用的一个JavaScript框架,它可以让我们更轻松地管理页面的状态、渲染和交互。但是在编写Vue应用时,有时会遇到“SyntaxError:Unexpectedtoken”报错,这个错误提示意味着代码中存在语法错误,JavaScript引擎

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor