12.URL重写
- 为什么要URL重写?1.有利于SEO(搜索引擎优化),带参数的RUL权重较低。2.地址看起来更正规,推广uid. 如我们一般在访问网页是会带参数,http://aaa.com/view.htm?id=1...,用URL重写后可以这样访问http://aaa.com/view-1.htm就可以访问参数id=1的页面,但实际上view-1.htm是不存在的,这个处理是在全局文件的Application_BeginRequest 事件 中处理后导向id=1的页面的。
- 伪静态:看起来像普通页面,而非动态生成的页面。
- 原理:在Global.asax的Application_BeginRequest中读取Request.Url得到请求的URL(View-3.aspx),然后用HttpContext.Current.RewritePath(ReWriteUrl)进行重写(View.aspx?id=3格式)
- 也可以使用微软的URLRewrite,只要修改配置文件就可以进行URL重写。
- 检查一个网站被SEO保存了搜索页多少次,可以在百度或google中输入site:(域名)例如(site:sina.com)查看被seo的次数。
示例:
分别建立两个页面,一个为view.htm,另一个为WebForm1.aspx,当我们在客户端输入http://localhot:8080/view-1.aspx是就访问view.htm页面,如果是view-2.aspx时就访问WebForm1.aspx页面。
开发步骤:
1.建立view.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body> <a href="http://www.sina.com">新浪</a> </body>
</html>
2.建立一WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Global.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar> </div>
</form>
</body>
</html>
3.添加Global.asax文件,并写入以下事件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Text.RegularExpressions; namespace Global
{
public class Global : System.Web.HttpApplication
{ protected void Application_Start(object sender, EventArgs e)
{ } protected void Session_Start(object sender, EventArgs e)
{ } protected void Application_BeginRequest(object sender, EventArgs e)
{
//在客户端输入view-1.aspx或view-2.aspx分别重定位到相关的页面处理
Regex reg = new Regex(@".+view-(\d+).aspx");
var match = reg.Match(HttpContext.Current.Request.Url.AbsolutePath);
if (match.Success)
{
string id = match.Groups[].Value;
if (id == "")
HttpContext.Current.RewritePath("view.htm");
else
HttpContext.Current.RewritePath("WebForm1.aspx");
}
} protected void Application_AuthenticateRequest(object sender, EventArgs e)
{ } protected void Application_Error(object sender, EventArgs e)
{ } protected void Session_End(object sender, EventArgs e)
{ } protected void Application_End(object sender, EventArgs e)
{ }
}
}
4.运行截图
12.URL重写的更多相关文章
- 【Head First Servlets and JSP】笔记12:URL重写
1.会话管理的核心在于“交换会话ID”,来回传递cookie是最简单的方法,容器通过客户端发来的JSSESIONID查找匹配的对话. 2.如果浏览器禁用了cookie,那就意味着浏览器将忽略响应首部中 ...
- 12 nginx URL 重写 ecshop案例
一:URL 重写 ecshop案例 Rewrite语法 Rewrite 正则表达式 定向后的位置 模式 Goods-3.html ---->Goods.php?goods_id=3 goods- ...
- Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解
转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...
- 用.htaccess文件实现URL重写
注:第一部分来自 http://www.cnblogs.com/wangkongming/archive/2012/11/13/2768251.html 这位博主的个人网站简洁 还有诗歌 ...
- cookie session URL重写 与考试
状态管理.Cookie.Session.URL重写 HTTP协议:无状态的连接(每次连接都是新的请求)1.隐藏字段 <input type="hidden" name=&qu ...
- C# WebService URL重写
背景 有时候我们会有这样的需求,将 WebService URL 中的 asmx 后缀去掉:或者我们要模拟普通 Web 的 URL,接口名称直接拼接在 URL 中.这些情况我们都要用到URL重写. 关 ...
- Nginx反向代理、负载均衡、页面缓存、URL重写及读写分离详解
大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负载均衡 六.Nginx之页面缓存 七.Nginx之URL重写 八.Nginx之读写分离 注,操作系统 ...
- Struts2进行url重写
一般来说我们在用Struts2进行开发的时候我们的访问url都是带上一些类似于.action或者.do还有用?传递参数,这种访问方式与静态页面的访问相比,我就可以用一些开源的组件来进行url的重写,以 ...
- ASP.net的url重写
http://blog.csdn.net/windok2004/article/details/2432691 1. 有关于URL的重写,本文也只是拿来主意.相继有MS的组件“URLRewriter” ...
随机推荐
- C# 类是怎么执行的?
C# 类是怎么执行的? public class Person{ static person(){} //不写,默认也有个空的 public person(){}//不写,默认也有个空的 public ...
- EDMX更新实体后出现键值映射问题
近期做项目的EF改版时,在DB(ORACLE)中的表里添加一个新的PK,去除原有的PK. 在DB已添加完成操作,但这时在EDMX里进行从DB更新到EF里,更新完成后就发生如下错误提示: Error 6 ...
- DBHelper 数据库帮助类
/// <summary> /// 数据库帮助类 /// <author>vito</author> /// </summary> public cla ...
- 如何在Angular2中使用Forms
在Angular2中形成两个基本对象是Control和ControlGroup. 控制用户输入 Control 一个控制表示一个输入字段(ngFormControl) 一个控制可以绑定到一个input ...
- Spring配置,JDBC数据源及事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ...
- Poj 1511 Invitation Cards(spfa)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 24460 Accepted: 8091 De ...
- 【ADO.NET】2、各种版本的 简单登录验证
一.简单登录验证(防SQL注入) GetString(序号) 返回某一列的值(当用户不记得列名序号时,可使用GetOrdinal()获取到序号)GetInt32(序号) 针对的是 int 字段,返回i ...
- Javascript数组的indexOf()、lastIndexOf()方法
在javascript数组中提供了两个方法来对数组进行查找,这两个方法分别为indexOf(),lastIndexOf(). 这两个方法都有两个参数,第一个参数为需要查找的项,第二个参数则是查找的起始 ...
- varnish 4.0编译安装小记
varnish 4.0 编译问题 centos-6.5 x86环境 装varnish遇到几个错误要先安装python-docutils然后提示error1,于是安装:libedit-devel然后提示 ...