Request.Url.AbsoluteUri and rewritten URL's - asp.net

I need to be able to get the URL as I see it in the browser i.e The rewritten one. For instance:
If my Url was www.myurl.com/Test.html and I then used Request.Url.AbsoluteUri, the URL returned would be: www.myurl.com/Default.aspx?Action=Test
I need to be able to get back the exact rewritten URL.
Does anyone know how I can achieve this?

From Tchami:
Have you tried Request.RawUrl? I think that gives you the unmapped URL.
Will mark this as the answer when I am allowed.

You could rewrite the URL so, that it includes the original URL as a querystring parameter.
For example: url="(.*)" to="http://newurl.com?original=$1".
(Note you'll still have to adjust the regex to work with URLs that contain a querystring.)

Related

Regex for fixing URL patterns

I have the following url structure:
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=334
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=1094
and so on..
Recently I used IIS rewrite to rewrite this structure as
http://www.xyxyxyxyx.com/productcategory/334/my-product-url
http://www.xyxyxyxyx.com/productcategory/1094/some-other-product-url
and so on..
This works fine.
I want to create another rule so that if an invalid url requests comes with the following structure:
http://www.xyxyxyxyx.com/productcategory/ShowProduct.aspx?ID=334
the 'productcategory' part should be removed from the url and the url should look like
http://www.xyxyxyxyx.com/ShowProduct.aspx?ID=334
How do I write this rule?
It may vary depending on what you are using to apply the regex, but here's a basic one:
's|productcatgory/||'
If you want to make sure it also only does this when the xyxyxyxyx url is present, this should work:
's|^http://www\.xyxyxyxyx\.com/productcategory/|http://www\.xyxyxyxyx\.com/|'
Edit: Ah, so if productcategory could be any category, then you'll need to match around it, like so:
's|^http://www\.xyxyxyxyx\.com/.*/ShowProduct|http://www\.xyxyxyxyx\.com/ShowProduct|'

How to remove the "?" character from g-wan URIs

I have checked cache.c <- totally clueless what it is doing or how to have pretty permalinks to servlet calls.
Update: OK, I know what the above does, but the problem is you have to call the above script first before you can access it as permalink. Is there any way I can access permalinks without using "?" at all (in the first place)?
I have also checked on this link: Anatomy of G-WAN URI servlets
I would like to have http://example.com:8080/servlet/arg1/arg2, without "?", and would like the above link to reference "servlet" to servlet.c.
Basically, like this pretty URL for this question
https://stackoverflow.com/questions/27084626/how-to-remove-in-g-wan-url-completely
See...no "?" within the URL.
Is this possible?
I have also checked
u8 *query_char = (u8*)get_env(argv, QUERY_CHAR);
*query_char = '!'; // use "/!hello.c" instead of "/?hello.c"
I know I can't do
*query_char = '';
you can re-write url with handler there is a simple rewrite example

Having two ? in a URL - Is it valid?

Here is the thing:
Let's say I have a base URL like this:
www.mysite.com?my_first_param=1&my_second_param=2
But now I want to add a new parameter to my URL:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com
Imagine that I am using a service where they add new tracking parameters to my URL. For example:
first_tracking=value1&second_value=value2
Since my BASE url already has a "?" the parameters are added with a "&", so my final URL would look like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com&first_tracking=value1&second_value=value2
This is a correct URL with several parameters, but when I do the redirect to www.myothersite.com, since the parameters start with a "&", they get lost. Would it be correct to add the tracking parameters with a starting "?" ? Like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com?first_tracking=value1&second_value=value2
If not, what would be a good approach for dealing with this? I believe is responsibility of the redirect to pass through the tracking parameters to the redirect URL.
You should URLencode each param name and value properly, so if the map of params you want is this:
my_first_param => 1
my_second_param => 2
my_redirect_site => www.myothersite.com?first_tracking=value1&second_value=value2
then you should pass this as the query string:
?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com%3Ffirst_tracking%3Dvalue1%26second_value%3Dvalue2
You should be using a library which does this already to build up URIs to handle this encoding for you.
No, you can't have a second question mark in a URL.
Furthermore, if you have ampersands in the redirect URL, they will be seen as separate parameters for the main URL, and not seen as connected to the redirect URL.
If you want to do a redirect like this, you need to URLEncode the whole of the redirected URL. There are standard functions in most web-facing languages to do this.
Encode the parameter, "?" would be replaced by %3F
It depends on the amount of control you have over the service adding the tracking parameters.
Can you change the url after the parameters have been added?
If you can, then you should use a url builder to add the tracking parameters to your redirect url, then url encode that url entriely, tracking parameters included.
If you are not in control and a third party modifies your url, then you will have to do this when you redirect, read the parameters in the url, take your redirect url and the tracking parameters, add the tracking parameters to the redirect url before redirect.

Rewrite Rule Config... Is this possible?

So I have a set of pages like so:
domain.com/product/?_d=1
domain.com/product/?_d=2
domain.com/product/?_d=3
I have set up a rewrite map for cleaner URLs like so
domain.com/shoes
domain.com/pants
domain.com/gloves
So now I have nice URLs for people. But what I also want to do is start forwarding the existing URLs to the newer URLs. So for anyone who goes to:
domain.com/product/?_d=1
I want them to be forwarded or rewrite the URL to:
domain.com/shoes
I tried adding in another map where I have the following:
Original: domain.com/product/?_d=1
New Value: domain.com/shoes
And I got a 404. I guess I understand why because the other map won't apply since I am using the first map. But how can I set up a map for the old URLs? Or can I? I know if my query string value was like _d=shoes I could just do a rewrite rule, but it's not like that. I guess I could change my page to be that way, but was just looking for another way.
I ended up creating a rewrite rule that looked for the pattern to the old URLs and forwarded them to the new URL. Then in my Server side code I just did a check for those legacy variables and redirected with a 301 accordingly.

Does Response.RedirectPermanent automatically pass querystring variables?

I have googled to death this to no avail. I have a page that we no longer want to use, however, dpending on how it was called, I want to pass the querystring along if there is one. I wrote some ugly code to add the querystring if there is one, but if the Response.RedirectPermanent already does it then I could skip that step. The microsoft web site is not clear. Thanks.
A 301 redirect will redirect to whatever you pass into RedirectPermanent, exactly that. It will not re-append the curent query string to what you are redirecting to.

Resources