ASP.NET中实现二级或多级域名(修改UrlRewrite)

3 views
Skip to first unread message

dinas...@gmail.com

unread,
Nov 20, 2006, 1:16:29 AM11/20/06
to WEB开发技巧回收站
大家应该知道,微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将
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


要实现这个功能,前提条件就是 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

的结果了

若你在实际应用中碰到了问题,请查看文章
"修改UrlRewrite以对域名进行重写"需要注意的问题
,希望能够帮助你!


附:

URLRewriter 的相关资料

http://jzywh.cnblogs.com/archive/2005/09/29/246650.html
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

dinas...@gmail.com

unread,
Nov 20, 2006, 1:20:00 AM11/20/06
to WEB开发技巧回收站
关于二级域名的泛化解析:

二级域名的泛解析是怎么实现

1、要求域名服务商(就是申请域名的地方)提供泛解析的功能。

2、加一个二级域名的解析,比如 *.16800000.com
指向到你的服务器的IP地址。

3、服务器的IIS新建一个站点,主机头留空即可。

4、写程序判断域名,根据不同的域名转到相应的页面就可以了。

这里可以用 Response.Redirect("")
缺点:URL地址就相应的改变。

也可以用 Server.Transfer("")
这个呢可以保持URL地址不变化,但是要注意网页里面图片的连接,很容易发生找不到图片的现象。

dinas...@gmail.com

unread,
Nov 20, 2006, 1:38:07 AM11/20/06
to WEB开发技巧回收站
解析无限个二级域名的方法有三种:
无论使用哪种,都必须使用域名泛解析。将*.test.com(此处我们test.com作例子)解析到你指定的服务器上。
如:
*.test.com 222.222.222.222

注:
作域名泛解析前,必须确认域名服务商对你提供域名泛解析服务。否则,后面的工作都是徒劳的。

方法一:使用Windows自带DNS解析。
步骤:
1,添加好test.com,如下图


2,在test下添加一个名称为 * 的域
(右键,添加域),添加完如下图


3,在*的域下,添加一个主机(右键,新建主机,主机名称为空,IP则填写为您要将域名泛解析的对应IP),添加完如下图。


解析完成,测试一下,Ping test.test.com
是不是解析到222.222.222.222

方法二:使用程序进行判断调整。
步骤:
1,iis服务的主机头留空,新建Default.asp文件,并把新建文件的执行优先级设定为最高(IIS设置属性中的文档,将Default.asp移到最上面)。
Default.asp文件代码:
<%
Dim iURL
iURL =
Split(Request.ServerVariables("SERVER_NAME"),".")
If Lcase(iURL(0))="www" Then
'此处为网站首页地址,请自行选择
Response.Redirect("index.asp")
Else

'如二级域名所调转地址,请自行更改此处地址
Response.Write( "<frameset><frame
src=""**.asp?"&iURL(0)&".index.html""></frameset>")
End If
%>

方法三:使用 ISAPI_Rewrite URL处理引擎
先介绍一下ISAPI_Rewrite :

ISAPI_Rewrite是一个强大的基于正则表达式的URL处理引擎。它非常类似于Apache's
mod_Rewrite,但它是专为IIS设计的。
ISAPI_Rewrite有两个版本:ISAPI_Rewrite Full与ISAPI_Rewrite
Lite。
ISAPI_Rewrite Lite是免费版本,但不支持反向代理功能。
ISAPI_Rewrite Full只能下载到30天的试用版本。
解析二级域名就可以用ISAPI_Rewrite一个规则来实现。
如:
test.test.com 映射成 www.test.com/test/
规则如下:
RewriteCond Host: (?!/.|www|ww)(.*).test.com
RewriteRule (.*) http/://www.test.com/$1$2 [I,R]

此三方法比较:
偶个人认为,方法一,解析速度会快点,但没有做过验证。只是感觉系统自带的东西会好些。
另,方法一和三,一台服务器可以做多域名的泛解析,而方法二,一台服务器只可以做一个域名的泛解析。

由于个人能力有限,参考了一下资料,编写此文。如有错误,欢迎大家指正。谢谢。

dinas...@gmail.com

unread,
Nov 20, 2006, 3:55:30 AM11/20/06
to WEB开发技巧回收站

dinas...@gmail.com

unread,
Nov 20, 2006, 4:28:16 AM11/20/06
to WEB开发技巧回收站

dinas...@gmail.com

unread,
Nov 20, 2006, 10:35:48 PM11/20/06
to WEB开发技巧回收站
1、HTTP 模块
2、事件
3、说明

1、FormsAuthenticationModule
2、AuthenticateRequest
3、
确定用户是否通过了窗体身份验证。如果没有,用户将被自动重定向到指定的登录页面。

1、FileAuthorizationMoudle
2、AuthorizeRequest
3、使用 Windows 身份验证时,此 HTTP 模块将检查以确保
Microsoft® Windows®
帐户对被请求的资源具有足够的权限。

1、UrlAuthorizationModule
2、AuthorizeRequest
3、检查以确保请求者可以访问指定的 URL。通过
Web.config 文件中的 <authorization> 和 <location> 元素来指定
URL 授权。


应该何时在 HTTP 模块中执行 URL
重写?这取决于要使用的身份验证类型。如果不想使用任何身份验证,则无论
URL 重写发生在 BeginRequest、AuthenticateRequest 还是
AuthorizeRequest
中都没有什么关系。如果要使用窗体身份验证而不使用
Windows 身份验证,请将 URL 重写放在 AuthorizeRequest
事件处理程序中执行。最后,如果要使用 Windows
身份验证,请在 BeginRequest 或 AuthenticateRequest
事件进行过程中安排 URL 重写。

Reply all
Reply to author
Forward
0 new messages