ASP.Net中的URL重写

时间:2009-08-21 07:48:08

标签: asp.net url-rewriting

我希望以用户可以使用username.domain.com访问网站的方式实现URL重写

e.g。
www.abc.com/login.aspx 我应该可以像访问它一样
www.username.abc.com/login.aspx

blogspot也是http://username.blogspot.com/

之类的例子之一

Plz建议我如何实现这一目标。

由于

3 个答案:

答案 0 :(得分:2)

基本上您需要做的是使用像Managed Fusion URL Rewriter and Reverse Proxy这样的工具,并遵循以下规则。

RewriteCond {HOST}    www\.(.*)\.abc\.com
RewriteRule ^/login.aspx$    /login.aspx?domain=%1
RewriteRule ^/login.aspx?domain=www\.(.*)\.abc\.com$  /login.aspx?user=$1

所以它将通过您的内部应用程序,如

URL: www.nick.abc.com/login.aspx
Internal URL: www.abc.com/login.aspx?user=nick

你必须解决的问题是你将如何获得用户名以及如何在内部处理它们。

但实际上你不需要URL重写器。您只需将所有DNS流量转发到同一IP地址,然后在应用程序中处理域,而不是通过DNS控制域。

答案 1 :(得分:0)

可以在服务器上安装ISAPI Rewrite

您必须将其放在网站的httpd.ini文件中

# Convert http://example.com to http://www.example.com/
RewriteCond Host: ^example.com
RewriteRule (.*) http\://www\.example.com$1 [I,RP]

# Assuming we have limited number of shared folders.
# We will execute them accordingly regardless of the subdomain.
# Example: http://sub1.example.com/img/logo.jpg -> /img/logo.jpg
# Example: http://www.example.com/img/logo.jpg -> /img/logo.jpg
RewriteRule (/css/.*) $1 [I,O,L]
RewriteRule (/js/.*) $1 [I,O,L]
RewriteRule (/img/.*) $1 [I,O,L]

#Redirect all other subdirectories not matching
#to the list above as subdomains
#example: www.example.com\sub1 -> sub1.example.com
RewriteCond Host: www\.example\.com
RewriteRule /(\w*)/(.*) http\://$1\.example\.com$2 [I,RP]

# If the web site starts with www then point the file to the root folder
# If you specifically created a folder /www/ then you can comment out this section.
RewriteCond Host: (?:www\.)example.com
RewriteRule (.*) $1 [I,O,L]

# Any web site starts other than www will be re-mapped to /<subdomain>/
# Example: http://sub1.example.com/default.asp -> /sub1/default.asp
# Note: if the folder does not exists, then the user will get a 404 error automatically.
RewriteCond Host: (.*)\.example.com
RewriteRule (.*) /$1$2 [I,O,L]

#Fix missing slash char on folders
#This has to be at the end because if invalid dir exists,
#we should show 404 first
RewriteCond Host: (.*)
RewriteRule ([^.?]+[^.?/]) http\://$1$2/ [I,RP]

至关重要的是这一个:

# Any web site starts other than www will be re-mapped to /<subdomain>/
# Example: http://sub1.example.com/default.asp -> /sub1/default.asp
# Note: if the folder does not exists, then the user will get a 404 error automatically.
RewriteCond Host: (.*)\.example.com
RewriteRule (.*) /$1$2 [I,O,L]

这是我能找到的最佳方式。如果您无法访问服务器并且没有安装ISAPI,那么这不适合您。

这是文章的链接 http://www.seoconsultants.com/windows/isapi/subdomains/

答案 2 :(得分:0)

在system.web

下的网络配置中尝试此操作
<system.web>
    <urlMappings enabled="true">
      <add url="~/myaccount" mappedUrl="myaccount.aspx"/>
    </urlMappings>

代码隐藏文件写入

Response.redirect("~/myaccount")`

这100%