网站301跳转__404页面代码
301跳转做法
asp的
<%
if Request.ServerVariables("HTTP_HOST") <> "https://www.360docs.net/doc/a36979025.html," then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","https://www.360docs.net/doc/a36979025.html,/"
Response.End
end if
%>
php的
if ($_SERVER['HTTP_HOST'] != 'https://www.360docs.net/doc/a36979025.html,'){
header("HTTP/1.1 301 Moved Permanently");
header("Location:https://www.360docs.net/doc/a36979025.html,/");
exit();
?>
1、IIS下301设置
Internet信息服务管理器 -> 虚拟目录 -> 重定向到URL,输入需要转向的目标URL,并选择“资源的永久重定向”。
2、ASP下的301转向代码
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “https://www.360docs.net/doc/a36979025.html,/articles/301/”
%>
3、https://www.360docs.net/doc/a36979025.html,下的301转向代码
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”https://www.360docs.net/doc/a36979025.html,/articles/301/“);
}
4、PHP下的301转向代码
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: https://www.360docs.net/doc/a36979025.html,/articles/301/”);
exit();
5、CGI Perl下的301转向代码
$q = new CGI;
print $q->redirect(”https://www.360docs.net/doc/a36979025.html,/”);
6、JSP下的301转向代码
<%
response.setStatus(301);
response.setHeader( “Location”, “https://www.360docs.net/doc/a36979025.html,/” );
response.setHeader( “Connection”, “close” );
%>
7、Apache下301转向代码
新建.htaccess文件,输入下列内容(需要开启mod_rewrite):
1)将不带WWW的域名转向到带WWW的域名下
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https://www.360docs.net/doc/a36979025.html, [NC]
RewriteRule ^(.*)$ https://www.360docs.net/doc/a36979025.html,/$1 [L,R=301]
2)重定向到新域名
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ https://www.360docs.net/doc/a36979025.html,/$1 [L,R=301]
3)使用正则进行301转向,实现伪静态
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+)\.html$ news.php?id=$1
将news.php?id=123这样的地址转向到news-123.html
8、Apache下vhosts.conf中配置301转向
为实现URL规范化,SEO通常将不带WWW的域名转向到带WWW域名,vhosts.conf中配置为:
ServerName https://www.360docs.net/doc/a36979025.html,
DocumentRoot /home/lesishu
ServerName https://www.360docs.net/doc/a36979025.html,
RedirectMatch permanent ^/(.*) https://www.360docs.net/doc/a36979025.html,/$1
第二种情况,如果你的系统是Linux的。
做整站301跳转,很简单。如下修改文件httpd.conf或者.htaccess文件保存就可以了:RewriteEngine On
RewriteCond %{HTTP_HOST} ^https://www.360docs.net/doc/a36979025.html, [NC]
RewriteRule ^(.*)$ https://www.360docs.net/doc/a36979025.html,/$1 [L,R=301]
第三种情况,如果你是虚拟主机用户,如果你有子目录,恭喜你,也可以整站301重定向。具体方法是:
1.新建404.asp,把下面代码放到新建的404文件中保存。
代码
<%@ Language=VBscript %>
<%
Dim PageURL
PageURL=“http://”&Request.ServerVariables("HTTP_HOST")&request.ServerVariables("U RL") &"?"&Request.ServerVariables("QUERY_STRING")
PageURL = Replace(PageURL, “https://www.360docs.net/doc/a36979025.html,/404.asp?404";https://www.360docs.net/doc/a36979025.html,:80,"") //修改为你的老域名
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", “https://www.360docs.net/doc/a36979025.html,”&PageURL&"" //修改为你的域名%><%@ language="VBscript">
<%@ language="VBscript">
2.新建index.asp,把下面代码放到新建的ind文件中保存。
代码
<%@ Language=VBscript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","https://www.360docs.net/doc/a36979025.html,/"//修改为你的域名。
%><%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "https://www.360docs.net/doc/a36979025.html,/"//修改为你的域名。
%>
3.用ftp在网站根目录新建文件夹名为301,把上面的index.asp和40
4.asp上传进去。然后打开虚拟主机控制面板,添加子目录,把域名https://www.360docs.net/doc/a36979025.html,绑定到301文件夹,绑定后设置所属404页面为404.asp:
第四种情况,如果你是虚拟主机用户且你没有子目录,缺银子呗,呵呵,也是可以整站301的,具体方法是:
在网站根目录http.ini文件中,添加如下代码:
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
RewriteCond Host:^xxxx\.com$
RewriteRule (.*) http\://www\.xxxx\.com$1 [I,RP)
404页面的正确做法
1、Apache服务器404页面的设置方法
在.htaccess 文件中加入代码:ErrorDocument 404 /Error.html,建立一个简单的html404页面命名 Error.html,把 Error.html放置在网站根目录即可。
2、IIS/https://www.360docs.net/doc/a36979025.html,下设置404错误页面
首先,修改应用程序根目录的设置,打开 "web.config" 文件编辑,在其中加入如下内容:
view sourceprint?
1
2
3
4
5
6
7
此例中"error.asp"为系统默认的404页面,"notfound.asp"为自定义的404页面,使用时请修改相应文件名。然后,在自定义的404页面"notfound.asp"中加入:view sourceprint?
1 <%
2 Response.Status = "404 Not Found"
3 %>
这样,便可以保证IIS能够正确地返回"404"状态码
HTML及网页制作练习题-完整版
1、在HTML中,()标记不可出现在
和标记符之间。(C) (A)(B)
(C)