Sankar M
Sankar M

Reputation: 4809

How to do URL rewriting for the URL to hide filename.aspx and querystring?

https://mysite.com/Admin/Dashboard/DashboardReport.aspx - this is my actual URL i want to hide the filename.aspx with if any querystring values. how?

also i have tried FriendlyURL in URL rewriting in IIS. it doesn't take effect.

Pls give me a quick reply.

Upvotes: 2

Views: 5372

Answers (2)

Suamere
Suamere

Reputation: 6258

URL Rewriting is somewhat ambiguous. Are you rewriting what the user sees? Or what the server sees? It's better to believe that you are not able to alter what the URL displays to the website consumer.

However, the trick is to send them to a broken, not-real URL... then ReWrite the URL for YOU, so you know what that not-real URL means. For example:

Imagine you have a Real URL like so:

http://www.example.com/folder/subfolder/page.ext?param=value

But you want to "Rewrite" the URL to look like so:

http://www.example.com/subfolder/value

If they are on the page page.ext..., you cannot change what they see in their URL bar. But, you can Redirect Them to a fake path /subfolder/value like so:

<rule name="subFolder Value Redirect">
  <conditions>
    <add input="{REQUEST_URI}" pattern="^/folder/([^/]+)/page\.ext\?param=([^&]+)$" />
  </conditions>
  <action type="Redirect" url="/{C:1}/{C:2}" />
</rule>

Above, I am not doing a "url match" because I am depending solely on a Condition. My only Condition is that the Request_URI looks exactly like I expect. Some folder, some subfolder name, page.ext, and a querystring with param=SomeValue. I use () parenthesis to dynamically capture the Subfolder Name and the param Value.

Note: {C:0} means the "0th" Capture Group of the Conditions. Capture 0 is the Entire Result of the regex ^/folder/([^/]+)/page\.ext\?param=([^&]+)$. Capture 1 is the First Capture Group, the Subfolder Name. Capture 2 is the second Capture Group, the param Value.

^ at the beginning of a regex means "starting from the beginning". Sometimes you don't want to start at the beginning and that wouldn't be there.

$ at the end of a regex means "ending at the end". Sometimes you don't want to end at the end and that wouldn't be there.

The Character Class [^...] means to capture any character that is not .... In this case, capture anything that is not a Slash, or anything that is not an Ampersand.

The repetition operator [...]+ Plus-Symbol means to capture the preceding rule at least once, but up to many times.

. is a special character which means "anything". But we mean it as in the dot of an extension page.ext. So you escape it like \.

{REQUEST_URI} means to compare your pattern against the entire URL after the Domain/Port. So in the URL http://www.example.com:1234/subfolder... The /subfolder and all following contents are in the {REQUEST_URI} EXCEPT hashtag routing. So if you have an Anchor or Angular UI Route, those stay with the browser only, and will always be at the end of any redirect, but the server will never see them, so you can't parse off of hashtags.

action type="Redirect" just means this action is to redirect the user to a given URL.

url="/{C:1}/{C:2}" is the URL relative to the domain, since it doesn't start with a protocol. This will redirect to /subFolder/value, because Condition capture 1 and Condition capture 2 in our pattern capture those two values.

After inserting this rule, a user will be redirected to a broken janked up URL that looks like a State-Representative URL (RESTful). Yaay!!

URL Rewriting

The rewrite is for you (the server), not for the customer. But now that they are redirected to a nice pretty broken URL, you can find the context of that URL and tell your server what it REALLY means by rewriting it for the server.

<rule name="subFolder Value Rewrite">
  <conditions><add input="{REQUEST_URI}" pattern="^/([^/]+)/([a-zA-Z0-9]+)$" /></conditions>
  <action type="Rewrite" url="/folder/{C:1}/page.ext?param={C:2}" />
</rule>

This is pretty much the inverse of the prior rule. This finds a pattern where there is a path "subFolder" name, slash, and a value made up of letters and numbers. It also, once again, describes the Begginning ^ and End $, because we don't expect unexpected weird things. So, this will catch a URL like:

http://www.example.com/subFolder/1234abcDE

But will not catch

http://www.example.com/lol/somethingelse/15?blah=kthx

The rewrite URL then evaluates back to a true real URL that maps to a path for processing for a file that really exists. And, keep in mind, that "rewrite" means it takes the "incorrect" fake value that the user sees, and rewrites it back to an ugly "correct" value for the server. Not the other way around.

Upvotes: 1

Bryan Naegele
Bryan Naegele

Reputation: 660

You want to use rewrite rules and the rewrite module.

http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/

Upvotes: 1

Related Questions