Forum Moderators: open

Message Too Old, No Replies

IIS, ASP.NET and 301 Redirect

301 redirect

         

ariu

8:50 pm on Jan 22, 2010 (gmt 0)

10+ Year Member



here's my situation.

I have a old site all in html. It's been rewritten in asp.net and it's going to be hosted on a new server (running windows).

I want a way to have all old html pages redirect to new aspx pages.

In the new server there will be no html pages. questions is, how do I tell IIS to redirect for instance:

1. contact.html -> contact.aspx
2. home.html -> home.aspx

and so forth.

PLEASE REMEMBER, there is no html pages in the new server.

Thanks in advance.

marcel

9:10 pm on Jan 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ariu, and welcome to WebmasterWorld [webmasterworld.com]

If you are running IIS7, then I would recommend the IIS rewrite module [iis.net], otherwise, something like ISAPI Rewrite will be your best bet.

ariu

9:24 pm on Jan 22, 2010 (gmt 0)

10+ Year Member



Thank you Marcel. I am running IIS 6, could you please elaborate on the second or point me to some resources to show me how to do it?

Thanks again

marcel

6:09 am on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ISAPI Rewrite [isapirewrite.com] is one of the best rewrite tools available for < IIS6.

This rule should do what you need

RewriteRule ^([^?]+)\.html(\?.*)? $1.aspx$2 [NC, R=301]

Another options is to add some more rules, removing the file extension altogether (ie. example.com/products/ instead of example.com/products.aspx). Giving you neater URL's and avoiding this problem in the future.

ariu

6:48 am on Jan 23, 2010 (gmt 0)

10+ Year Member



Thanks for the example. I did take a look at it and it looks very good. I'm certainly going to give it a try.

One question about url rewrite, if I have a url that looks like:

products.aspx?productid=20 and I use a rewrite rule to have a cleaner url, will my code fail because it uses a line like this:

productID = Request.QueryString["productid"].ToString();

Thanks again.

marcel

8:07 am on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...will my code fail...

Not if the rewrite is set up correctly, this is basically how it works, consider the following rewrite:

RewriteRule ^product/(\d+)/ /productinfo.aspx?productID=$1

This will catch the following URL:
example.com/product/123/

and rewrite this to:
example.com/productinfo.aspx?productID=123

in your productinfo.aspx page you can Request the QueryString as you normally would.

Basically, the URL in the address bar of the user stays as it was (example.com/product/123/) and in the background IIS actually calls the productinfo.aspx page.

Hope that helps

- BTW your code example

productID = Request.QueryString["productid"].ToString();
doesn't need the ToString() as a querystring is already a string. If you are expecting an integer I would suggest converting it to Int32, ie
int productID = Convert.ToInt32(Request.QueryString["productid"]); 

This will cause an exception if the QueryString is not an integer, safeguarding you from malicious users trying to find a weakness in your code.

ariu

7:17 pm on Jan 24, 2010 (gmt 0)

10+ Year Member



Thank you so much, that was very helpful.

One more question, if I may.

what syntax is used in case of more than one parameters.

For instance I have:

default.aspx?cmd=products&productID=10

Thanks again, you've been a great help.

marcel

8:07 am on Jan 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Each Regular Expression 'Rule' is attributed a number in order of appearance; $1, $2, $3 etc.

Your example

default.aspx?cmd=products&productID=10
could have a rule looking like this:

RewriteRule ^([^/]*)/(\d+)/ /default.aspx?cmd=$1&productID=$2

The examples I have given may not be syntactically correct, I do not use ISAPI Rewrite myself so I cannot test them. You can find more info and examples here [helicontech.com]