IIS Rewriting with Query String and Question Mark IIS 7 - iis-7

I have read the threads and I still can't work it out, my problem I know, but wondering if could point me in the right direction.
I have setup a rule:
www.mydomain.com/productlist.asp?CategoryID=2
rewritten to
www.mydomain.com/homeware/cushions
.
<rule name="cushions">
<match url="^homeware/cushions" />
<action type="Rewrite" url="productlist.asp?CategoryID=2" />
</rule>
ok, now my problem is with pagination of the results, so the original domain when the user goes onto the next page would look like:
www.mydomain.com/productlist.asp?CategoryID=2&Page=2
This is where I'm having problems - I want this to become:
www.mydomain.com/homeware/cushions?page=2
and ongoing for how many pages
just can't get it to work - I understand I need to use query string but really struggling. Asking for expertise, thanks for any help

To capture the Query String you need to use conditions for that since the match URL does not include it.
So you could do that with a rule like:
<rule name="cushions" stopProcessing="true">
<match url="^homeware/cushions$" />
<conditions>
<add input="{QUERY_STRING}" pattern="page=(\d+)" />
</conditions>
<action type="Rewrite" url="productlist.asp?CategoryID=2&page={C:1}" appendQueryString="false" />

Just add appendQueryString="true" in action will automatically append query string.
<rewrite>
<rules>
<rule name="test">
<match url="^homeware/cushions" />
<action type="Rewrite" url="/test/test.cfm?category=5" appendQueryString="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
http://localhost:8081/homeware/cushions?page=2
You will receive page in URL variable with value 2.

Related

IIS rewrite URL in asp.net goes to into infinity loop

I just need to create a rule in my web config file to rewrite all URLs to given into rule
ex:
url/services/presentations-evenementielles to
url/Services/Presentations-Evenementielles
This is all made for SEO purposes to avoid duplicates.
<rule name="ProductionRule2" stopProcessing="true" patternSyntax="ExactMatch" >
<match url="^/productions/xyz" ignoreCase="true" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent"/>
</rule>
Above code its gives me infinite loop error.
There was some mistakes with your rule
1.You were trying to use "^" in an exact match rule. Please set it to regular expression
2.You are trying to use "/" in match url. The match url part of domain/productions/xyz is
productions/xyz instead of /production/xyz.
3.You are enabling ignore case when you redirect URL to itself.
So please try to modify the rule like this
<rule name="ProductionRule2" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^productions/xyz" ignoreCase="false" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent" />
</rule>
Update:
Please modify the rule like this
<rule name="ProductionRule2" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^Productions/XYZ" ignoreCase="false" negate="true" />
<action type="Redirect" url="Productions/XYZ" redirectType="Permanent" />
<conditions>
<add input="{ToLower:{URL}}" pattern="/productions/xyz" />
</conditions>
</rule>
And this is working on my side
Please remember to clean browser cache when you test the rule.And the URL should be prODuctions/Xyz instead of prODuction/Xyz.

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>

IIS / web.config url redirect rule based on regex is not triggered

Here is my web.config rule
<rule name="spiderRedirect" stopProcessing="true">
<match url=".post/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z-]+)" />
<action type="Redirect" url="https://example.net/bot/post.php?category={toLower:{R:1}}&id={toLower:{R:2}}&title={toLower:{R:3}}" appendQueryString="false" />
</rule>
This should give this result
input:
https://example.net/post/celebrity/4cHYQ7i/maisie-williams
should result in:
{R1}: celebrity
{R2}: 4cHYQ7i
{R3}: maisie-williams
and it should redirect to:
https://example.net/bot/post.php?category=celebrity&id=4cHYQ7i&title=maisie-williams
This input:
https://example.net/post/nerdy/NE5cHQZ/when-i-have-to-do-technical-support
should result in:
{R1}: nerdy
{R2}: NE5cHQZ
{R3}: when-i-have-to-do-technical-support
The server runs and I am quite sure the regex is correct, but the rule is never triggered. I am never redirected to google, even if I use the above input.
Why isn't the rule triggered?
I got it working. Also with check for User agent.
I use this to redirect web crawlers.
<rule name="spiderRedirect" stopProcessing="true">
<match url=".?post/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z-]+)" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="(Google|MSNBot|Twitterbot|Pinterest|Facebot|facebookexternalhit|MJ12bot|bingbot|SimplePie|SiteLockSpider|okhttp|curl|YandexBot|ScoutJet|Slurp|DuckDuckBot|Baiduspider|Sogou|Konqueror|Exabot|ia_archiver|Screaming)"/>
<add input="{HTTPS}" pattern="on"/>
</conditions>
<action type="Redirect" url="https://example.net/bot/post.php?category={toLower:{R:1}}&id={toLower:{R:2}}&title={toLower:{R:3}}" appendQueryString="false" />
</rule>
You will escape "/" to matching your different input. The new regex is :
([0-9a-zA-Z]+)\/([0-9a-zA-Z]+)\/([0-9a-zA-Z-]+)
You can test it on https://regex101.com.
It match for your two different input.

How to Debug URL Rewrite Rules in ASP.NET Web.config [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
[UPDATED QUESTION - 2020-03-06]
Here is my problem.
I am working on an ASP.NET website, where the URL rewrite rules are very complex.
As a result, it is difficult to discover which rules are matching for a particular URL.
I would like help finding the ASP.NET classes, code or events that process the rules in the web.config so that I can trace through the process that takes place while the URL is being handled by IIS.
I want to be able to see the URL and the result of each attempt to match a rule.
Does anyone know a debugging technique to use in the Visual Studio debugger or some type of print statements that I can use to trace this process?
Here is the documentation for the rewrite rules: URL Rewrite Module Configuration Reference
Below is an example that I have already debugged, showing the result of understanding the rules. The rule "New2" is matched when testing.
Example:
<system.webServer>
...
<rewrite>
<rules>
<rule name="Mobile">
<match url="^mobile$" ignoreCase="false" />
<action type="Rewrite" url="mobile.aspx" />
</rule>
<rule name="Print rule">
<match url="^([A-Za-z0-9/_-]+)/print$" ignoreCase="false" />
<action type="Rewrite" url="print.aspx?url={R:1}" appendQueryString="true" />
</rule>
<rule name="New paging1">
<match url="^([A-Za-z0-9/_-]+)/([A-Za-z0-9/_-]+)/([0-9]+)/page/([0-9]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?parent={R:1}&url={R:2}&godina={R:3}&page={R:4}" appendQueryString="false" />
</rule>
<rule name="New paging2">
<match url="^([A-Za-z0-9/_-]+)/([0-9]+)/page/([0-9]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?url={R:1}&godina={R:2}&page={R:3}" appendQueryString="false" />
</rule>
<rule name="Page rule 1">
<match url="^([A-Za-z0-9/_-]+)/([A-Za-z0-9/_-]+)/page/([0-9]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?parent={R:1}&url={R:2}&page={R:3}" appendQueryString="false" />
</rule>
<rule name="Page rule">
<match url="^([A-Za-z0-9/_-]+)/page/([0-9]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?url={R:1}&page={R:2}" appendQueryString="true" />
</rule>
<rule name="New">
<match url="^([A-Za-z0-9/_-]+)/([A-Za-z0-9/_-]+)/([0-9]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?parent={R:1}&url={R:2}&godina={R:3}" appendQueryString="false" />
</rule>
<rule name="New2">
<match url="^([A-Za-z0-9/_-]+)/([0-9]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?url={R:1}&godina={R:2}" appendQueryString="false" />
</rule>
<rule name="rule 1">
<match url="^([A-Za-z0-9/_-]+)/([A-Za-z0-9_-]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?parent={R:1}&url={R:2}" appendQueryString="true" />
</rule>
<rule name="Glavni rule 2">
<match url="^([A-Za-z0-9/_-]+)$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
...
You can test your rules in your IIS Manager and it's quite simple. In short:
open your IIS Manager, click needed website
on central pane, in 'IIS' block, double click on 'URL Rewrite' icon - you will see a list of created in web.config rules (or empty
list if they don't exist)
select the rule you want to test, find 'Edit' button on right pane in 'Inbound Rules' block
'Edit Inbound Rule' page will be opened, find 'Test pattern' button and push
here you can enter different urls in 'Input data to test' field (having 'Pattern' field filled) and click on 'Test' button to check
the expression over your url, results will be shown below.
Full manual here https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/testing-rewrite-rule-patterns
You can use Failed Request Tracing for this. It helps me a lot. This article describe how to use it in url rewrite. Use IE to view log files.
Thanks
This is what I found after month of googling.
I looks poor but worked for me ...

IIS 7 URL Rewrite

How do I rewrite everything after the question mark (?) as one parameter?
For example, I have a url as: http://www.example.com?abcdefg/test/module?wiating4request
Notice after the first question mark we have another in the query string. I basically need to post to that url and I cannot modify the url so I need to make do with what is provided.
I saw something similar here: How can I use mod_rewrite to remove everything after the ? (question mark) in a URL?
Keep in mind this is for IIS 7.
Any ideas?
It's possible to do this with a rewrite rule in case you only want to match URL's with just one question mark too many, like your example. You can then use this rule:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Clean extra question mark from query string" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^(.+?)(\?(.+))*$" />
</conditions>
<action type="Rewrite" url="/{URL}?{C:1}&{C:3}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
If you want to match an unlimited number of too many question marks I think you will need to revert to a custom rewrite provider as detailed in the linked article. You might then end up with something like:
<rule name="Clean extra question mark from query string" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{QUERY_STRING}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="/{URL}?{ReplaceProvider:{QUERY_STRING}}" appendQueryString="false" />
</rule>

Resources