IIS 7.5 URL Rewrite Module to Reformat Links?

677 Views Asked by At

I'm using the IIS Rewrite Module to take care of the migration between blogs so that no links are broken in the process. So I'm really just using a lot of 301 redirects.

However, I'd like to avoid redirects when possible for performance and SEO reasons. I can do this by duplicating my rules in code. But I thought I'd ask first. Is there a way to reuse the rules for the rewrite module to preprocess and reformat links?

The following is an excerpt from the blog theme...

<a rel="bookmark" href="<%=Post.PermaLink %>" title="<%=Server.HtmlEncode(Post.Title) %>">Permalink</a>

I'd like to change this to something like href="ReformatLink(Post.PermaLink)", where ReformatLink runs the url rewrite rules on the specified URL and returns the new URL.

2

There are 2 best solutions below

0
On BEST ANSWER

I found another way. I can create exactly the link format I want by modifying BlogEngine.Core. Inside Post.cs I simply change the properties PermaLink and RelativeLink to reflect the link structure of the old blog software.

I'll leave my rewrite module rules in place for now, in case I've missed something. But this seems to take care of the problem.

0
On

I'm using this Library http://www.iis.net/download/urlrewrite and I am doing something like the below.

    <rule name="FolderAndNoQueryString" stopProcessing="true">
      <match url="^([^/]+)/([^/]+)/?$" />
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <!--  The following condition prevents rule from rewriting requests to .axd files -->
        <add input="{URL}" negate="true" pattern="\.axd$" />
      </conditions>
      <action type="Rewrite" url="{R:1}/{R:2}.aspx" />
    </rule>

In my HTML code I have the following;

<a href="<%=AppSettings("ApplicationPath") %>Folder/MyActualASPXPage">My Link</a>

ApplicationPath is just a normal Web.Config Key as shown below;

<add key="ApplicationPath" value="/MyVirtualDir/" />