Convert QueryString to regular Url

  • Thread starter Thread starter David C. Taylor
  • Start date Start date
D

David C. Taylor

I saw an article that described how to convert the query string into
an Url without a query string and return the page.

For example, this request:

http://www.mywebsite.com/products.aspx?category=motherboard

becomes something like this:

http://www.mywebsite.com/motherboard.aspx

I need to do that or something like it. The problem is, I cannot put
my hands on that article. It could have been anywhere from a dozen
magazines, dozens of websites, or dozens of newsletters. I really
wish I had bookmarked it. If anyone can point me to any resources
describing this technique I would be highly appreciative.

Regards,

David Taylor
 
You can use the request.RawURL() method to return the entire query, then use
some string functions to back out everything up to the "?".

Chip
 
David,

I haven't seen the article your looking for but my guess is that it involves
creating a custom 404 page which then redirects to the appropriate aspx page
(passing in all the appropriate querystring parameters). These are good but
make a bit of overhead which you want to avoid on larger sites.

Another way of doing this without all the overhead is to use an ISAPI filter
to process your incoming requests and then direct them to the correct page.
I use ISAPI rewrite on some of the sites we develop. Its been really useful
with Google indexing of the sites and page rankings. Have a look at
www.isapirewrite.com

Happy Coding!

J
 
Could you be a little more descriptive about how you are trying to get this
to work? Obviously you could do something like

response.redirect(request.queryString("category") & ".aspx")

but I have a feeling you are trying to do something a little more
complicated.

-D
 
Thanks for the input guys. The problem is not manipulating the
string, the problem is generating and sending the page as a response
because in my example, the page:

http://www.mywebsite.com/motherboard.aspx

doesn't actually exist but is an 'alias' for

http://www.mywebsite.com/products.aspx?category=motherboard

so somehow it is using members of a namespace near or under good old

System.Web.UI.Page

perhaps the Request or Response members.

Thanks for your responses, I'll keep looking into this.

David
 
Ah...

At first I was thinking you could do a redirect to a custom error page by
modifying the customErrors element of your web config, but I think you'll
have to trap it in the Application_Error event of your Global.asax file,
otherwise you can't know what the requested script name is. When an error
occurs you can do something like:

(psudo-code)

IF currentError = filenotfoundexception then
'request the server variables for the current httprequest
'grab the script name and remove the extention
'do a response.redirect to the page using the new parameter
'OR
'flush the response buffer and do a server.execute to the page.
end if

There may be a more elegant way to do this that you probably read, but that
would be my 2 cents. And FYI, It would seem to me that you are asking for
confusion by doing something like this. ;o)

-D
 
Hi David,

As I understand, you want to type in the IE address window:
http://www.mywebsite.com/motherboard.aspx (which is not exist)
Then you want to capture this error and redirect to:
http://www.mywebsite.com/products.aspx?category=motherboard

To get this done, I think you should change the error page of 404 page not
found to your customized page my404.html.
This is the content of my404.html:
<script>

DocURL = document.URL;

if (DocURL=="http://www.mywebsite.com/motherboard.aspx"){

NewDocURL ="http://www.mywebsite.com/products.aspx?category=motherboard";

window.navigate(NewDocURL);

}

</script>

To change the default of the 404 error page, you should follow bellow steps:
a. Run Internet Service Manger (If you’re using IIS 4.0, you can run it at
Start -> Programs -> Windows NT 4.0 Option Pack -> Microsoft Internet
Information Server -> Internet Service Manager)

b. Expand the Default Web Site and choose the corresponding virtual
directory vd1.
c. Right click vd1 and click Properties.

d. Click Custom Errors Tab and choose the 404 item in the list.

e. Change the Contents to point to the file my404.html

f. Click OK


Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: (e-mail address removed) (David C. Taylor)
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Re: Convert QueryString to regular Url
| Date: 22 Oct 2003 11:46:27 -0700
| Organization: http://groups.google.com
| Lines: 42
| Message-ID: <[email protected]>
| References: <[email protected]>
| NNTP-Posting-Host: 208.16.173.127
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1066848388 23632 127.0.0.1 (22 Oct 2003
18:46:28 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Wed, 22 Oct 2003 18:46:28 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.su
l.t-online.de!t-online.de!diablo.theplanet.net!news.maxwell.syr.edu!postnews
1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:185859
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thanks for the input guys. The problem is not manipulating the
| string, the problem is generating and sending the page as a response
| because in my example, the page:
|
| http://www.mywebsite.com/motherboard.aspx
|
| doesn't actually exist but is an 'alias' for
|
| http://www.mywebsite.com/products.aspx?category=motherboard
|
| so somehow it is using members of a namespace near or under good old
|
| System.Web.UI.Page
|
| perhaps the Request or Response members.
|
| Thanks for your responses, I'll keep looking into this.
|
| David
|
|
| (e-mail address removed) (David C. Taylor) wrote in message
| > I saw an article that described how to convert the query string into
| > an Url without a query string and return the page.
| >
| > For example, this request:
| >
| > http://www.mywebsite.com/products.aspx?category=motherboard
| >
| > becomes something like this:
| >
| > http://www.mywebsite.com/motherboard.aspx
| >
| > I need to do that or something like it. The problem is, I cannot put
| > my hands on that article. It could have been anywhere from a dozen
| > magazines, dozens of websites, or dozens of newsletters. I really
| > wish I had bookmarked it. If anyone can point me to any resources
| > describing this technique I would be highly appreciative.
| >
| > Regards,
| >
| > David Taylor
|
 
David said:
I saw an article that described how to convert the query string into
an Url without a query string and return the page.

For example, this request:

http://www.mywebsite.com/products.aspx?category=motherboard

becomes something like this:

http://www.mywebsite.com/motherboard.aspx

I need to do that or something like it. The problem is, I cannot put
my hands on that article. It could have been anywhere from a dozen
magazines, dozens of websites, or dozens of newsletters. I really
wish I had bookmarked it. If anyone can point me to any resources
describing this technique I would be highly appreciative.

Regards,

David Taylor

What you want to do is called "URL rewriting". You might want to do a
Google search on that term - there are a lot of techniques out there.

One specific article that may be of interest to you is:


http://msdn.microsoft.com/msdnmag/issues/02/08/HTTPFilters/default.aspx
 
Hi ,

I think the link you provide of much value, but I think there are some
limitation.
It can only handle the request already reached your application, but if
David want to do something like this:

the customer types:
http://www.mywebsite.com/motherboard.aspx (this page does not exist)

David wants it to redirect to
http://www.mywebsite.com/virtual1/virtual2/virtual3/test.aspx

I think your web application in /virtual1/virtual2/ will not get the
request, so it can not set the Http filter for the request.
So in this situation, you need to change the 404 error page in root
direcotry to your customized page.

Anyway, I think the httpfilter way of handling the request is a more
elegant way.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Date: Thu, 23 Oct 2003 07:58:51 -0700
| From: mikeb <[email protected]>
| User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5b)
Gecko/20030901 Thunderbird/0.2
| X-Accept-Language: en-us, en
| MIME-Version: 1.0
| Subject: Re: Convert QueryString to regular Url
| References: <[email protected]>
| In-Reply-To: <[email protected]>
| Content-Type: text/plain; charset=us-ascii; format=flowed
| Content-Transfer-Encoding: 7bit
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: 12.8.192.60
| Lines: 1
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186068
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| David C. Taylor wrote:
| > I saw an article that described how to convert the query string into
| > an Url without a query string and return the page.
| >
| > For example, this request:
| >
| > http://www.mywebsite.com/products.aspx?category=motherboard
| >
| > becomes something like this:
| >
| > http://www.mywebsite.com/motherboard.aspx
| >
| > I need to do that or something like it. The problem is, I cannot put
| > my hands on that article. It could have been anywhere from a dozen
| > magazines, dozens of websites, or dozens of newsletters. I really
| > wish I had bookmarked it. If anyone can point me to any resources
| > describing this technique I would be highly appreciative.
| >
| > Regards,
| >
| > David Taylor
|
| What you want to do is called "URL rewriting". You might want to do a
| Google search on that term - there are a lot of techniques out there.
|
| One specific article that may be of interest to you is:
|
|
| http://msdn.microsoft.com/msdnmag/issues/02/08/HTTPFilters/default.aspx
|
| --
| mikeb
|
|
 
Thanks to mikeb for providing an answer to my inquiry. I was
searching using keywords like QueryString reformat, redirect, Url,
alias, HttpRequest, etc., and not getting any results. When using the
correct term, "url rewriting" in quotes as a search query, that does
the trick.

McD mentioned that I might be creating extra confusion by tring to do
this, and that is a valid point to the developer. However, there are
at least two reasons to want to do this which are, I believe, 'worth
it' if a robust solution can be found.

First, which url is much more user friendly (remember MOST people on
the web are not developers or even slightly tech savy)?

http://www.mydomain.com/results.aspx?product=easterbunny

or

http://www.mydomain.com/easterbunny.aspx

I think the latter url is more user friendly, easier to transcribe and
bookmark.

Secondly, we have heard that most/all (?) search engines will not
crawl urls with a query string and thus we want to eliminate those
urls as much as reasonable.

Having said that, the link to the MSDN article provided by mikeb

http://msdn.microsoft.com/msdnmag/issues/02/08/HTTPFilters/default.aspx

is very informative but neither of the solutions presented fit exactly
what I was looking for. Well worth the read though.

Using "url rewriting" google did turn up the original article I was
looking for which was indeed what I needed to solve my problem. The
article at Code Project is "URL Rewriting with ASP.NET" by Richard
Birkby.

http://www.codeproject.com/aspnet/URLRewriter.asp

I had searched The Code Project but had the wrong keywords.The
solution is nice and uses HttpContext.RewritePath to change the Url.

Thanks all, David Taylor
 
Back
Top