Forum Moderators: open
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.
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.
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.
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.
...will my code fail...
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"]); 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]