Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm writing HTML pages for a single page application, and want to publish it to my current ASP hosting, so I'm using the IIS URL Rewrite module to send a lot of requests to /index.html. I'm sending them like this:

<rule name="Section">
  <match url="sections/(.*)" />
  <action type="Rewrite" url="index.html?section={R:1}" logRewrittenUrl="true" />
</rule>

What I'd like to do is get the rewritten query string with JavaScript, though this seems like an impossibility. Is there a way in a static html page to get the rewritten query string? window.location.href is the original url, not the rewritten one.

share|improve this question
Could you give four example urls - two entered urls and the resulting rewritten ones? And clarify which url you would like to get via javascript? – threeFourOneSixOneThree 2 days ago
blah.com/sections/range -> blah.com/index.html?section=range, blah.com/sections/input -> blah.com/index.html?section=input. I'd like to get the rewritten query string, if possible (I realize I could parse it out of the original url, and will do so if not possible). – Ehryk 2 days ago
Could you use the Response Headers to include the new rewritten url .../index.html?section=range in the http header and read it in via javascript? – threeFourOneSixOneThree 2 days ago

1 Answer

up vote 1 down vote accepted

The easiest way to do it is to simply dump the rewritten URL as part of the response body:

<input type="hidden" id="__rewritten_url" value="<%=Request.ServerVariables("URL")%>" />

Alternatively, you can append the rewritten URL as a Response Header then issue an Ajax request and extract that header from it. so your web.config would look something like:

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Section">
                 <match url="sections/(.*)" />
                 <action type="Rewrite" url="index.html?section={R:1}" logRewrittenUrl="true" />
            </rule>
        </rules>
        <outboundRules>
            <rule name="AppendRewrittenResponseHeader" patternSyntax="Wildcard">
                <match serverVariable="RESPONSE_URL" pattern="*" />
                <action type="Rewrite" value="{URL}" />
            </rule>
        </outboundRules>
    </rewrite>
</system.webServer>

Then in your client side (using jQuery for brevity):

$.ajax({
        type: "HEAD",
        url: location.href,
        success: function(message,text,response)
              {
              var myRewrittenUrl = response.getResponseHeader('URL');
              }
    });
share|improve this answer
It's a static .html page, I'm only using IIS because I have ASP hosting. The second solution might work though, thanks! – Ehryk 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.