URL Re-Writing in ASP.Net

旧街凉风 提交于 2020-01-16 16:30:23

问题


I want to implemewnt URL-Rewriting in way that user can access website with username.domain.com

e.g.
www.abc.com/login.aspx
I should be able to access this like
www.username.abc.com/login.aspx

blogspot is also one of the example like http://username.blogspot.com/

Plz suggest me how can I accomplish this.

Thanks


回答1:


Basically what you need to do is use a tool like the Managed Fusion URL Rewriter and Reverse Proxy, with the following rule.

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

So it will come through to your internal application like

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

The thing you have to solve which you didn't address is how are you going to get the users name and how are you going to handle them internally.

But really you don't need a URL Rewriter. You just forward all DNS traffic to the same IP address, and then you handle the domain with in your application instead of controlling it through the DNS.




回答2:


It is possible with ISAPI Rewrite installed on the server

You have to put this in the httpd.ini file of the website

# 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]

The vital part is this one:

# 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]

This is the best way I could find. If you don't have access to the server sittings and don't have ISAPI installed then this is not for you.

Here's the link to the article http://www.seoconsultants.com/windows/isapi/subdomains/




回答3:


Try this in web config under system.web

<system.web>
    <urlMappings enabled="true">
      <add url="~/myaccount" mappedUrl="myaccount.aspx"/>
    </urlMappings>

in code behind file write

Response.redirect("~/myaccount")`

This works 100%



来源:https://stackoverflow.com/questions/1310561/url-re-writing-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!