0

I've installed the IIS Rewrite 2.0 module via Web Components on my Windows Server 2012.

I've read several articles but I just can't seem to get my simple rewrite to work.

I would like to rewrite http://www.acme.com/news/13/Jan/20 to http://www.acme.com/news.html#20-Jan-13

This is the rule I'm using:

<rule name="news articles" stopProcessing="true">
    <match url="^news\/(.*)\/(.*)\/(.*)$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="news.html#{R:3}-{R:2}-{R:1}" appendQueryString="false" />
</rule>

When I apply http://www.acme.com/news/13/Jan/20 as my test pattern the rewrite works.

However, if I browse to http://www.acme.com/news/13/Jan/20 I get a 404 error:

Requested URL      http://www.acme.com/news.html#20-Jan-13
Physical Path      C:\Webs\acme.com\www\news.html#20-Jan-13

The physical file news.html exists and I can browse to it directly.

Is it the that are messing things up? Clearly C:\Webs\acme.com\www\news.html#20-Jan-13 isn't a physical file but I don't know how to solve this problem.

I can of course browse directly to http://www.acme.com/news.html#20-Jan-13 without issue.

Can anyone assist please?

Many thanks in advance.

Cheers, Mark

Mark
  • 687
  • 7
  • 12
  • Are you sure it's not a permissions issue on the file/folder? – d_ethier Apr 21 '13 at 14:24
  • Thanks d_ethier. No, all the files in the web root, including news.html, can be loaded by the browser. My website has been up for months now; I've only just decided to create friendly URLs. All my other rewrite rules are working - just this one isn't. – Mark Apr 21 '13 at 22:52

1 Answers1

0

According to this "How do write a rewrite rule in IIS that allows HTML anchors?" answer, browsers do not send anything after the # in a URL request to the server. They're markers intended solely for the browser to scroll to on the resulting page.

If you look at the logs, do you see a full request for the page including the anchor tag?

An alternative might be to use some DOM loaded JS to scroll the page to the anchor referenced in the URL (jQuery required):

$(function () {
  var a_tag = $("a[name='" + window.location.hash.replace("#", "") +']");
  $('html,body').animate({scrollTop: a_tag.offset().top}, 'slow');
});

Although, that likely doesn't solve the intended issue of friendlier URLs.

Community
  • 1
  • 1
d_ethier
  • 3,873
  • 24
  • 31
  • Thanks for the effort d_ethier, but that's not quite what I'm looking for, but it might have to do :) Yes, the logs show the web server trying to load the file news.html#dd-mmm-yy (which of course doesn't exist). So after much searching I've come to the conclusion that one cannot rewrite to a page anchor (but you can redirect - which doesn't allow the 'friendly' url to remain in-situ). So I think I'll have to solve my issue using traditional params (eg. news.html?d=dd&m=mm&y=yy) and then some jQuery (like you suggest) to convert the params into an on-page anchor and scroll to it. – Mark Apr 22 '13 at 01:08