天天看點

用URLRewriter實作URL和域名重寫

http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

首先下載下傳這個安裝,把URLRewriter.dll複制到web的bin目錄下面。

然後在web.config裡面進行如下配置

<configSections>

<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

</configSections>

<RewriterConfig>

<Rules>

<!-- 産品制表者規則 -->

<RewriterRule>

<LookFor>~/Products/Beverages/.aspx</LookFor>

<SendTo>~/functions/displaypro.aspx?mid=1</SendTo>

</RewriterRule>

</Rules>

</RewriterConfig>

<httpHandlers>

<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

</httpHandlers>

如果是IIS6.需要對網站做以下配置

站點屬性-》主目錄-》配置-》添加

C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll

.html

取消“确認檔案是否存在”前的勾

如果是II7。需對網站做以下配置

網站的處理程式映射裡面的對應.aspx的映射設定通路限制,作用設定為未指定.

域名重寫。

首先要把網站域名泛解析到你本機。

然後對URLRewriter的源代碼裡面的RewriterFactoryHandler.cs進行修改。

public class RewriterFactoryHandler : IHttpHandlerFactory

{

/// <summary>

/// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run. The job of

/// GetHandler is to return an instance of an HttpHandler that can process the page.

/// </summary>

/// <param name="context">The HttpContext for this request.</param>

/// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param>

/// <param name="url">The RawUrl of the requested resource.</param>

/// <param name="pathTranslated">The physical path to the requested resource.</param>

/// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned

/// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates

/// to.</returns>

public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)

{

// log info to the Trace object...

context.Trace.Write("RewriterFactoryHandler", "Entering RewriterFactoryHandler"); //string sendToUrl = url;url重寫

string sendToUrl = context.Request.Url.AbsolutePath;//域名重寫

string filePath = pathTranslated;

// get the configuration rules

RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules; // iterate through the rules

for (int i = 0; i < rules.Count; i++)

{

// Get the pattern to look for (and resolve its URL)

string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules.LookFor) + "$"; // Create a regular expression object that ignores case...

Regex re = new Regex(lookFor, RegexOptions.IgnoreCase); // Check to see if we've found a match

//if (re.IsMatch(url))//URL重寫

if (re.IsMatch(context.Request.Url.AbsolutePath))//域名重寫

{

// do any replacement needed

//sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(url, rules.SendTo));//url重寫

sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(context.Request.Url.AbsolutePath, rules.SendTo));//域名重寫 // log info to the Trace object...

context.Trace.Write("RewriterFactoryHandler", "Found match, rewriting to " + sendToUrl); // Rewrite the path, getting the querystring-less url and the physical file path

string sendToUrlLessQString;

RewriterUtils.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath); // return a compiled version of the page

context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler"); // log info to the Trace object...

return PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context);

}

}