web.config Redirect Rule - Match URL - asp.net

This seems to be a pretty straight forward change, however it's not matching the URL and none of the answers on stackoverflow seem to address this simple use of redirect rules. I believe it has something to do with the '?id=XXXX' portion of the URL.
We have some old versions of pages, which I am trying to add redirects to the new version of the pages.
Here's an example of my rules:
<rule name="old_Page" stopProcessing="true">
<match url="^Page.aspx?id=12345"/>
<action type="Redirect" url="http://www.example.com/newPage.aspx" redirectType="Permanent" />
</rule>
Any help would be most appreciated.

I used a different method to create Static Redirects in the web.config from this article:
https://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module
<rewrites>
<rules>
<rule name="Redirect Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.example.com{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="Page.aspx?id=12345" value="/newPage.aspx" />
</rewriteMap>
</rewriteMaps>
</rewrites>
I can add as many redirects as I like in the rewrite map section by adding additional keys.

Related

Http to Https URL rewriting

My requirement is straight forward. i have Http://abc need to make it Https://abc . I have added the folowing code in web.config. i.e. added new rule in IIS.
I have folowwed the URL rewriting module in IIS.
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
But still it doesn't work for me. Help me out.
The code you are using is very similar to what I use in production except that I use redirectType="Permanent" and I happen to use a hard coded domain name to ensure a canonical domain and I use {R:1} for the path and query but I think using {HTTP_HOST}{REQUEST_URI} as you are should work however you may need a slash between the two.
Give this a try:
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

URL rewrite for Google analytics

I have searched through many S.O. questions and answers, but haven't seen a good solution for this. I have already created and well-established SEO URLs in my web.config file. I was using a rewriteMap to basically turn my product pages into any URL that I needed.
Here is what I have to do this:
<rule name="Redirect Rule" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RedirectMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="Rewrite Rule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{RewriteMapName:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
What is being requested now is to add in Google Analytics parameters in order to track campaigns. If I simply add in the parameters, it will of course, give me a 404 error. I am now trying to get these SEO URLs to pick up these parameters, but getting quite frustrated.
If I remove the query string, Google Analytics will not pick it up since it is a client side script. Something like this might be close, but I am not pointing to any specific page.
URL Rewrite with Query String
This is how I remove the querystring:
<rule name="Subject redirect with query" stopProcessing="true">
<match url="^(.*)" />
<conditions trackAllCaptures="true">
<add input="{QUERY_STRING}" pattern="^utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Redirect" url="{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
Summed up, how do I turn this:
https://www.example.com/fancy-url
to this
https://www.example.com/fancy-url?utm_source=Targeted_Email&utm_medium=email&utm_campaign=campaigntag
After much frustration, I was able to figure this out. The query string was okay, but one of the settings was preventing the rest of the rules to be processed. After the rule name, I had stop processing set to true. When I looked at it in IIS, the setting is much more descriptive. It says "Stop processing of subsequent rules". Once I removed that and moved this above my other rules, worked perfectly.
<rule name="Google Query string">
<match url="(.*)$" />
<conditions>
<add input="{QUERY_STRING}" pattern="utm_source=([^=&]+)&utm_medium=([^=&]+)&utm_campaign=([^=&]+)&?(.*)$" />
</conditions>
<action type="Rewrite" url="{R:0}" appendQueryString="false" />
</rule>

wordpress post redirect rule not working IIS / coldfusion

I'm trying to redirect wordpress posts to a coldfusion page by using an IIS URL redirect. the posts link is
domain.com/?p=345
so I have set up the following redirect using the rolling Pattern
/?p=([0-9]+)
To point to the following page...
/blog.cfm?ID={R:1}
But sadly when I check out the page, it simply refreshes and doesn't redirect to the blog.cfm page.
Any help or advice much appreciated.
Below is the full web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="wp post redirect" stopProcessing="true">
<match url="/?p=([0-9]+)" />
<action type="Redirect" url="blog.cfm?ID={R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Your rule should be like that:
<rule name="wp post redirect" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{QUERY_STRING}" pattern="p=([0-9]+)" />
</conditions>
<action type="Redirect" url="blog.cfm?ID={c:1}" appendQueryString="false" />
</rule>
Explanation:
Your rule had mistake <match url= contains only url path without query string.
<match url="^$" /> that means apply this rule for requests, which valid for ^$ regexp. It is only for homepage
<add input="{QUERY_STRING}" pattern="p=([0-9]+)" /> that means apply this condition. Only if query string valid for p=([0-9]+) regexp
<action type="Redirect" url="blog.cfm?ID={c:1}" appendQueryString="false" /> Redirect to log.cfm?ID={c:1} where {c:1} is first match from regexp from condition

IIS 7.5 Url Rewrite ignoring question mark

I'm trying to create a rewrite rule in IIS 7.5 Url Rewrite. What I'm trying to achieve is when someone clicks on a certain pdf file, I want them to be redirected to a form and then when they've filled out the form they get the pdf file which simply has a question mark.
e.g. ?download=true appended to the end of it that passes through the rewrite rule so:
pdf/my-pdf-file.pdf will be redirected to go-to-this-file.aspx (which would be a form)
then they're redirected to :
pdf/my-pdfpfile.pdf?download=true which the rewrite rule shouldn't pick up but it does which is my problem.
Here is my rule:
<system.webServer>
<rewrite>
<rules>
<rule name="My PDF Rule">
<match url="^pdf/my-pdf-file.pdf$" ignoreCase="true" />
<action type="Rewrite" url="/go-to-this-file.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
Please can someone help. Cheers!
The problem is simple, you should check if the {QUERY_STRING} contains download=true and then don't redirect. Try this:
<system.webServer>
<rewrite>
<rules>
<rule name="My PDF Rule">
<match url="^pdf/my-pdf-file.pdf$" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="download=true" negate="true" />
</conditions>
<action type="Rewrite" url="/go-to-this-file.aspx" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>

Bidirectional URL Rewriting/Redirecting in IIS7.5

First off, I'd like to apologise for the ludicrous title. I'm not trying to sound cool or clever by using the word 'bidirectional', I just genuinely couldn't think of another way to describe it. Promise.
On to my problem. I have the following in the <system.webserver>/<rewrite>/<rules> section of my Web.config.
<!-- Who We Are -->
<rule name="1A">
<match url="^whoweare\.aspx$" />
<action type="Redirect" url="who-we-are" redirectType="Permanent" />
</rule>
<rule name="1B">
<match url="^who-we-are$" />
<action type="Rewrite" url="whoweare.aspx" />
</rule>
<!-- What We Do -->
<rule name="2A">
<match url="^whatwedo\.aspx$" />
<action type="Redirect" url="what-we-do" redirectType="Permanent" />
</rule>
<rule name="2B">
<match url="^what-we-do$" />
<action type="Rewrite" url="whatwedo.aspx" />
</rule>
Now this works tremendously. Effectively, if you visit the URL http://example.com/whoweare.aspx (which is the actual URL of the page), you'll be 301 redirected to the URL http://example.com/who-we-are (the virtual URL), and if you visit the virtual URL, you'll be rewritten to the actual URL.
This means super sexy URLs without duplication, and it doesn't result in reciprocal rewriting either, so smiles all round.
My question is this: could this be done more elegantly?
It's a little cumbersome having to write out two rules to ensure that one is redirected to the other, and the other is rewritten to the one. Is it possible to write one rule which will achieve the functionality of the above two?
Elegance is a subjective term, I guess there are a couple of ways that would be better, such as using Rewrite extensibility and implement fancy mapping logic, but by far the way I would recommend is using just 2 rules, one for Redirect and one for Rewrite, and with that just leverage Rewrite Maps that will make it a bit more readable (but still painful) to manage them, for example below you would now only need to maintain the maps and never have to deal with rules anymore:
<system.webServer>
<rewrite>
<rules>
<rule name="Rewrite From Pretty URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URLsToRewrite:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="Redirect To Pretty URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URLsToRedirect:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="URLsToRewrite">
<add key="/who-we-are" value="/whoweare.aspx" />
<add key="/what-we-do" value="/whatwedo.aspx" />
</rewriteMap>
<rewriteMap name="URLsToRedirect">
<add key="/whoweare.aspx" value="/who-we-are" />
<add key="/whatwedo.aspx" value="/what-we-do" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

Resources