Dave Mackey
Dave Mackey

Reputation: 4432

Subdirectory URL Automatic Rewrite Rule?

I'm using ASP.NET 4 / VB.NET / IIS to build a web application. Folks will access the web application via a URL like so:

http://url.com/personname

So, for example, here is the urls for Jane Doe, John Doe, and James Doe respectively:

http://url.com/janedoe

http://url.com/johndoe

http://url.com/jamesdoe

I need to somehow pass them off (no matter what name is entered) to a default.aspx page which then pulls in the name portion of the url and uses it to perform a lookup against the database which contains info. about this particular person and displays a personalized page based on that information.

So, for example, Jane Doe visits:

http://url.com/janedoe

She is transparently redirected to default.aspx which pulls in her name (from the url above) and checks it against the database. Seeing that she has never before visited the site she is transparently redirected to welcome.aspx which asks her to register for an account, says, "Hello Jane!" and so on...

The next time Jane visits she goes to:

http://url.com/janedoe

She is transparently redirected to default.aspx which pulls in her name (from the url above) and checks it against the database. Seeing that she has visited before and has created an account she is transparently redirected to login.aspx which asks her to enter the authentication credentials she created last time.

Upvotes: 1

Views: 470

Answers (1)

Olaf
Olaf

Reputation: 10247

First, you need to rewrite the request to Default.aspx. Depending on your IIS version (the following is for IIS 7.5), that is simple. In web.config, the rule (in section system.webServer) would be something like

<rewrite>
    <rules>
        <rule name="test" enabled="true" stopProcessing="false">
            <match url="^([a-zA-Z]*)$" />
            <action type="Rewrite" url="Default.aspx?u={R:1}" />
        </rule>
    </rules>
</rewrite>

(or you can use the IIS manager). The regex [a-zA-Z] should, of course, be adapted to suit your business rules - you may want to allow numbers.

That rewrites the request inside IIS - nothing is seen in the browser. Now you want to "transparently redirect" it, which means the URL in the browser should change depending on if the user already exists or not.

To achieve that, in Default.aspx code behind you process Request.QueryString["u"], follow by

Response.Redirect("Welcome.aspx") or
Response.Redirect("Login.aspx")

depending on the database query result. If you don't want to change the browser URL, use Server.Transfer instead of Response.Redirect.

Upvotes: 1

Related Questions