C# url重写及二级域名

it2022-05-08  8

=======================================Url重写==========================================

大家在网上看到不少诸如http://www.xundee.com/tuku/

http://www.xundee.com/photo/25.html

这种列表类型的url以及http://www.xundee.com/html/20081027/2555.shtml  

等这种详细页面的url。

首先感觉,这种urll很短,隐藏实际路径提高安全性;易于用户记忆和键入; 易于被搜索引擎收录。

事实也正式如此,尤其便于搜索引擎收录以及提高安全(比如:注入攻击等)。

当然,程序直接生成静态页面可以实现的,但是那样要生成很多这种页面以及文件夹,而且在数据量大的情况下生成是很浪费时间的,在这种情况下就要实现url重写了,当然url重写有很多种方法下面介绍的是基于asp.net下微软的url重写方法(URLRewriter方法)

1.首先下载URLRewriter.dll,下载之后直接复制到你的网站的bin目录中下载地址  http://www.xundee.com/download/URLRewriter.rar2.配置你的web.config文件在<configuration>节点下加<configSections>    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />  </configSections>

<RewriterConfig>    <Rules>     以下三个是我自己写的测试的,当然你可以写很多个,这些都是url重写的规则,根据你写的规则,实现不同的功能     这个是把http://www.xundee.com/default.aspx?id=1 生成http://www.xundee.com/photo/25.html 这种页面     <RewriterRule>        <LookFor>~/photo/(\d{0,8})\.html</LookFor>        <SendTo>~/list/photo.aspx?id=$1</SendTo>      </RewriterRule>    这个是把http://www.xundee.com/search/default.aspx 生成http://www.xundee.com/search.html 这种页面       <RewriterRule>        <LookFor>~/search\.html</LookFor>        <SendTo>~/search/default.aspx</SendTo>      </RewriterRule>    这个是把http://www.xundee.com/list/default.aspx?id=10&page=1 生成http://www.xundee.com/list/10_1.html这种页面,分页的时候使用这种url重写规则      <RewriterRule>        <LookFor>~/list/(\d{0,8})_(\d{0,8})\.html</LookFor>        <SendTo>~/list/default.aspx?id=$1&page=$2</SendTo>      </RewriterRule>

注意在web.config中把&换为&否则不能执行

    </Rules>

  </RewriterConfig>

同时还要在<system.web>配置节下加      <httpHandlers>        <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />        <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

      </httpHandlers>

3.好了,基本上就可以了,但是还有重要一点千万不要忘了因为咱们重写的是诸如http://www.xundee.com/photo/25.html

.html的后缀所以需要在iis中进行.html映射(如果咱们上面的链接不用html,直接使用aspx的,如:http://www.xundee.com/search/default.aspx这种的,就不用实现.html映射,当然,如果想使用别的后缀,如.do等,可以进行.do的映射),方法如下:右击站点-属性-主目录-配置在打开页面找扩展名.aspx,双击之后弹出窗口内复制可执行文件一般是C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll复制完之后点添加在弹出窗口把内容复制到可执行文件扩展名.html当然这个你可以任意写了,不过要和<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />保持一致.同时去掉确认文件是否存在,确定即可完成之后输入http://www.xundee.com/list/33.html是不是就可以访问了?爽吧

4.下面介绍第三种情况,生成目录,诸如这种url:http://www.xundee.com/tuku/没有后缀名  http://www.xundee.com/search/?keywords=%u7684  也没有后缀名的url这里面稍微有点复杂,同样配置iis还是在那个弹出窗口,点插入把复制的文件即C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll复制到可执行文件框内,同时去掉确认文件是否存在,确定即可这样输入http://www.xundee.com/archiver/,是不是就可以访问了?嘿嘿可能读者会有疑问了:现在设置的这个和第二种设置的有什么区别?现在设置的其实是进行*映射,即把所有的文件都进行url重写处理程序处理这时其实就可以把.html 以及自带的.aspx等映射去掉,同样可以生成.html/.aspx/ url好了,如果还想其他的扩展大家就尽情扩展吧。

5.现在再讲一下*映射要注意的问题因为*映射是把所有的文件都要交给url称谢程序去处理,这样就会造成会把站点下的图片以及css或者一些下载的rar等文件都要去处理,这样会慢吗?答案是毫无疑问的,肯定会增加内存的,单单一网站的图片以及上传的图片就足以让程序减慢成倍的速度了。那有什么办法进行处理吗?当然,我们可以进行以下处理就可以把速度还原了:方法1,把存放图片所在的文件夹转化成一个虚拟目录,在然后移处该虚拟目录*.*的映射,这样一来他的url重写程序就不会去处理图片文件,当然存放别的不需要url重写程序的文件也要象图片文件夹做类似的处理。虚拟目录设置办法:在iis站点右击目录点属性,目录-点创建然后点配置,在弹出窗口内删除:通配符应用程序映射(执行顺序)下添加的*.*的映射即可。方法2,把不需要*.*映射的文件夹设置新站点和原来的站点分开,这样在原来的站点访问的时候用新站点的域名即可。比如把图片设置站点http://images.xundee.com/ 这样在http://xundee.com/站点访问图片的时候就必须使用http://images.xundee.com/图片名称.gif,这样就避免了所有问件处理。以上两种方法即可提高不少速度了,当然第一种药方便的多。

6.最后处理回发,即是指当我们在一个经过url重写的页面的提交返回后,一般情况下又返回了真实的路径,而不是咱们处理过的url地址,这种情况就需要咱们在需要处理回发(即提交表单的页面)的页面加上以下代码即可:

 <script type="text/javascript">try{document.forms[0].action=""}catch(ex){}</script>

放在<form></form>里面

比如http://photo.xundee.com/photo/50/show_1.html  这个页面是经过url重写之后的,用户留言之后(提交表单)如果不做处理就变成了原始的路径了,如:http://photo.xundee.com/photo/Detail.aspx?ptid=50&page=1 所以我们要处理回发的情况。

 

 

=======================================二级域名======================================

微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 http://http://www.abc.com//1234/  重写为 http://www.abc.com/show.aspx?id=1234  但不能将 http://1234.abc.com/  重写为  http://www.abc.com/show.aspx?id=1234。 要实现这个功能,前提条件就是  http://www.abc.com/ 是泛解析的,再就是要修改一下URLRewriter了。 总共要修改2个文件 1.BaseModuleRewriter.cs

protected   virtual   void  BaseModuleRewriter_AuthorizeRequest( object  sender, EventArgs e)          {             HttpApplication app = (HttpApplication) sender;             Rewrite(app.Request.Path, app);         }

改为

protected   virtual   void  BaseModuleRewriter_AuthorizeRequest( object  sender, EventArgs e)          {             HttpApplication app = (HttpApplication) sender;             Rewrite(app.Request.Url.AbsoluteUri, app);         }

就是将  app.Request.Path 替换成了  app.Request.Url.AbsoluteUri

2.ModuleRewriter.cs for ( int  i  =   0 ; i  <  rules.Count; i ++ )              {                 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)                 string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";                 // Create a regex (note that IgnoreCase is set)                 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);                 // See if a match is found                 if (re.IsMatch(requestedPath))                 {                     // match found - do any replacement needed                     string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));                     // log rewriting information to the Trace object                     app.Context.Trace.Write("ModuleRewriter""Rewriting URL to " + sendToUrl);                     // Rewrite the URL                     RewriterUtils.RewriteUrl(app.Context, sendToUrl);                     break;        // exit the for loop                 }             } 改为 for ( int  i  =   0 ; i  <  rules.Count; i ++ )              {                 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)                 string lookFor = "^" + rules[i].LookFor + "$";                 // Create a regex (note that IgnoreCase is set)                 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);                 // See if a match is found                 if (re.IsMatch(requestedPath))                 {                     // match found - do any replacement needed                     string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));                     // log rewriting information to the Trace object                     app.Context.Trace.Write("ModuleRewriter""Rewriting URL to " + sendToUrl);                     // Rewrite the URL                     RewriterUtils.RewriteUrl(app.Context, sendToUrl);                     break;        // exit the for loop                 }             } 将 string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$"; 改成了 string lookFor = "^" + rules[i].LookFor + "$"; 完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。 再就是写web.config里的重写正则了 < RewriterRule >              < LookFor > http://(\d+)\.abc\.com/ </ LookFor >              < SendTo > /show.aspx?id=$1 </ SendTo >          </ RewriterRule >

在IE地址栏输入http://1234.abc.com/,就可以看到http://www.abc.com/show.aspx?id=1234

 

编译后的DLL下载:URLRewriter.rar

转载于:https://www.cnblogs.com/hayden/archive/2009/08/17/1548277.html


最新回复(0)