473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

What's the simplest way to do URL rewriting?

Hello,

I would like to be able to use an URL like ...

http://www.whatever.com/order/123

and have it translated into ...

http://www.whatever.com/orders.aspx?orderid=123

I have searched for articles on URL rewriting and found loads. The
trouble is, they're all so busy being clever telling you how to do
advanced things, that I can't work out how to do the most basic part!!
In this situation, the incoming URL will be fixed, apart from the number
of course and I always rewrite to the same real URL.

If anyone has a link to a really clear and simple article on the subject
(code in C# please), or even some code snippets, please let me know. I
just want to do this simple task first, I can worry about fancy stuff at
another time.

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
9 1982
Hi,

The most 'basic' part is calling either overload of:
HttpContext.Rewrite

I guess the reason why most articles appear tough is because they explain
URL Rewriting using HTTP Modules and Handlers.

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/

"Alan Silver" wrote:
Hello,

I would like to be able to use an URL like ...

http://www.whatever.com/order/123

and have it translated into ...

http://www.whatever.com/orders.aspx?orderid=123

I have searched for articles on URL rewriting and found loads. The
trouble is, they're all so busy being clever telling you how to do
advanced things, that I can't work out how to do the most basic part!!
In this situation, the incoming URL will be fixed, apart from the number
of course and I always rewrite to the same real URL.

If anyone has a link to a really clear and simple article on the subject
(code in C# please), or even some code snippets, please let me know. I
just want to do this simple task first, I can worry about fancy stuff at
another time.

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
>The most 'basic' part is calling either overload of:
HttpContext.Rewrite
Do you know where I can find more information about how to use it? The
SDK is pretty short on explanation.

I tried adding the following to a page ...

void Page_Load(Object sender,EventArgs e) {
//Create a 'Httpcontext' object using 'Current' property
HttpContext myContext = HttpContext.Current;
//Rewrite the internal path
myContext.RewritePath("/");
}

and loading it, but the page (which is not the site's root page) just
loaded as normal.

How would I achieve what I asked, ie allow the user to request
http://www.whatever.com/orders/123 and have this translated into
http://www.whatever.com/orders.aspx?orderid=123
I guess the reason why most articles appear tough is because they explain
URL Rewriting using HTTP Modules and Handlers.


Probably. That certainly made is confusing. Please could you explain how
the HttpContext.Rewrite is used without modules and handlers.

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3
Hi Alan:
I guess the reason why most articles appear tough is because they explain
URL Rewriting using HTTP Modules and Handlers.


Probably. That certainly made is confusing. Please could you explain how
the HttpContext.Rewrite is used without modules and handlers.


You'll have to use RewritePath before the request reaches the endpoint
(the page). That's why URL rewriting schemes use an HTTP handler - you
need to catch the request in the pipeline, like during the
BeginRequest event.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #4
>>>I guess the reason why most articles appear tough is because they explain
URL Rewriting using HTTP Modules and Handlers.


Probably. That certainly made is confusing. Please could you explain how
the HttpContext.Rewrite is used without modules and handlers.


You'll have to use RewritePath before the request reaches the endpoint
(the page). That's why URL rewriting schemes use an HTTP handler - you
need to catch the request in the pipeline, like during the
BeginRequest event.


So can I just put an Application_BeginRequest in Global.asax, check if
the URL in Request.Path matches what I want, and if so rewrite to the
real URL?

I'm not clear enough on the details to try this yet, but if this is
right, I think I understand how it works.

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #5
>I'm not clear enough on the details to try this yet, but if this is right, I
think I understand how it works.


Actually, I just thought about it some more and decided I did understand
it well enough to know where to try, and it worked!!

This is global.asax ...

<%@ Language="C#" %>

<script runat=server>

void Application_BeginRequest(Object sender , EventArgs e) {
string strPath = Request.Path.ToLower();
// Fake URLs will be like https://secure.pixata.co.uk/order123.aspx
// check if the path is to a customer order
if (strPath.StartsWith("/order")) {
// get the order number
strPath = strPath.Substring(6);
if (strPath.Length > 0) {
// so there is more than just /order, knock off the .aspx (if
there is one)
if (strPath.EndsWith(".aspx")) {
string strOrderID = strPath.Substring(0, strPath.Length - 5);
// now check if the order ID is a plain integer.
try {
int intOrderID = Convert.ToInt32(strOrderID);
// got this far, so it must be an integer
Context.RewritePath("/pdap/order.aspx?orderid=" + strOrderID);
} catch {
Response.Redirect("/");
}
}
} else {
Response.Redirect("/");
}
}
}
</Script>

The idea is that if someone enters http://whatever.com/order123.aspx it
will get rewritten to
http://whatever.com/order/pdap/order.aspx?orderid=123

Now, I have two issues left. The first is that I would really like to
use the URL...

http://whatever.com/order/123

without the .aspx, but this gives a page not found error, presumably
because the lack of .aspx extension means that IIS doesn't pass it to
the ASP.NET engine. Is there any simple way around this?

The bigger issue is that the /pdap/ folder (where the real .aspx page
resides) is protected by Forms authentication. At the moment, if someone
tries the URL http://whatever.com/order123.aspx and are not logged in,
it sends them to the log in page. I would prefer they get sent elsewhere
(say to the home page as the other failed requests do now).

I tried adding ...

if (User.Identity.IsAuthenticated) {

and only doing the rewriting if this works. If this is false, I redirect
as with other failures.

Unfortunately it doesn't work. Even when you are not logged in, you get
sent to the log in page.

Can I get round this? I don't want non-users to know there is a log in
page at all.

Thanks for any help

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #6
Hi Alan:

<snip>

Now, I have two issues left. The first is that I would really like to
use the URL...

http://whatever.com/order/123

without the .aspx, but this gives a page not found error, presumably
because the lack of .aspx extension means that IIS doesn't pass it to
the ASP.NET engine. Is there any simple way around this?

That one is a pain, one way to get it to work is to create the 123
folder and put a default.aspx document inside. Then IIS will find the
a default document (assuming default.aspx is in the list of defaults
in IIS) and pass control to ASP.NET. Hopefully IIS7 will avoid this
problem but that is a long ways off.
The bigger issue is that the /pdap/ folder (where the real .aspx page
resides) is protected by Forms authentication. At the moment, if someone
tries the URL http://whatever.com/order123.aspx and are not logged in,
it sends them to the log in page. I would prefer they get sent elsewhere
(say to the home page as the other failed requests do now).

I tried adding ...

if (User.Identity.IsAuthenticated) {


My guess is that because the user isn't authenticated until later in
the pipeline processing (AuthenticateRequest comes after BeginRequest)
that IsAuthenticated will always be false at that point. You might
want to push that login into the AuthorizeRequest event...

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #7
Alan Silver wrote:
The most 'basic' part is calling either overload of:
HttpContext.Rewrite


Do you know where I can find more information about how to use it? The
SDK is pretty short on explanation.


As usual, MSDN is the place to go:

http://msdn.microsoft.com/library/de.../en-us/dnaspp/
html/urlrewriting.asp

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #8
>> > The most 'basic' part is calling either overload of:
> HttpContext.Rewrite


Do you know where I can find more information about how to use it? The
SDK is pretty short on explanation.


As usual, MSDN is the place to go:

<snip>

Thanks, but I've read that article several times. As I explained right
at the beginning, I find most of the articles on the subject far too
complex. That one was no exception.

As I posted the other day, I finally managed to work out how to do the
simplest of URL rewriting, but it wasn't with the help of that article!!

Thanks for the link though. I usually find MSDN or the SDK to be fine,
but in this case, I found them to be overly complex.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #9
>>Now, I have two issues left. The first is that I would really like to
use the URL...

http://whatever.com/order/123

without the .aspx, but this gives a page not found error, presumably
because the lack of .aspx extension means that IIS doesn't pass it to
the ASP.NET engine. Is there any simple way around this?


That one is a pain, one way to get it to work is to create the 123
folder and put a default.aspx document inside. Then IIS will find the
a default document (assuming default.aspx is in the list of defaults
in IIS) and pass control to ASP.NET. Hopefully IIS7 will avoid this
problem but that is a long ways off.


Trouble with this is that the 123 is an order number, so will be
different for every order. I don't want to have to create a new folder
for every order.

Truth is, the .aspx extension isn't so terrible, I was just looking to
improve even more!! The bigger problem is the one below...
The bigger issue is that the /pdap/ folder (where the real .aspx page
resides) is protected by Forms authentication. At the moment, if someone
tries the URL http://whatever.com/order123.aspx and are not logged in,
it sends them to the log in page. I would prefer they get sent elsewhere
(say to the home page as the other failed requests do now).

I tried adding ...

if (User.Identity.IsAuthenticated) {


My guess is that because the user isn't authenticated until later in
the pipeline processing (AuthenticateRequest comes after BeginRequest)
that IsAuthenticated will always be false at that point. You might
want to push that login into the AuthorizeRequest event...


I thought that, but if I put the code in AuthenticateRequest, then I get
an error of "The resource cannot be found".

I realised that when they get sent to the log in page, the ReturnUrl
querystring variable is set to the page they requested, in this case the
one that generates the orders. In this case, all I need to do is check
for this in Page_Load and redirect to the home page. That way, if
someone (say a customer who sees the fake URL in the footer of a sales
invoice) types the URL in directly and isn't logged in, they will go
straight to the home page of the site and never know that there is an
orders page. Someone who is logged in will see the page correctly.

I'm not sure this is the neatest way to do it, but it sure works!!

Thanks for the reply.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: aling | last post by:
Given the bit field struct: int main() { union { struct { unsigned short s1 : 4; unsigned short s2 : 3;
3
by: Michael Appelmans | last post by:
I'm trying to use a rule based URL rewrite application which uses HttpApplication.RewritePath. I keep getting "rsource not found" error in application when running on shared web host although the...
4
by: darrel | last post by:
I'm building a site template that, hopefully, will be just one single ASPX page. There are 10 main districts (each getting a variation of the template), and I could easily pass that info via a...
3
by: Stephane | last post by:
Hi, I have a problem and I haven't found a good solution yet... I'm using Url rewriting for my message board so it's more readable to have something like mysite.com/1040/message.aspx instead...
3
by: Chris | last post by:
Can anyone give me any ideas on how to do URL rewriting in ASP with IIS 5 (possibly 6). Aside of course from switching to Apache ;) Regards, Chris. I want my products/product.asp?id=1243124 to read...
3
by: Smokey Grindel | last post by:
I am using ASP.NET 2.0 and know about the static list in the web.config for URL rewriting, but my list is dynamic and I am running into some problems... 1) How do you get URL rewriting to work in...
8
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi guys I'm having trouble with URL rewriting using HttpApplication.Context.RewritePath in a web application I've created. Everything works, but the links (css, images) in the pages break when...
2
by: Seth Williams | last post by:
The first scenario is that I want to create one website for a particular industry - let's say for Farm Machinery so I have a shared hosting site called myFarmMachinery.com But I want to sell...
1
Shinobi
by: Shinobi | last post by:
I am using ASP.net(c#) for my project. In my my project 2 pages are using URL rewriting method by referring this article URL Rewriting using Intelligencia UrlRewriter Example 1 - Blog Day...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.