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 ...
Related
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.
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>
I am trying to translate this apache rewrite rule into web.config rules but I can't get it to work.
Basically it checks the user agent and redirect the agent to the url provided
# allow social media crawlers to work by redirecting them to a server-rendered static version on the page
RewriteCond %{HTTP_USER_AGENT (facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)
RewriteRule qs/(\d*)$ http://sitetocrawl.com/doc?id=$1 [P]
This is what I have so far. However, I can't figure out how to catch the url querystring parameter. Basically the text string after http://example.com/qs/parameter
<rule name="Social Rewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="urltomatchpattern" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/[0-9]|Twitterbot|Pinterest|Google.*snippet" />
</conditions>
<action type="Redirect" url="http://sitetocrawl.com/doc?parameter" appendQueryString="true" redirectType="Found" />
</rule>
EDIT:
I tried with many variants of simpler rules, like redirect/rewrite when a specific user agent requests the site(in my case, the facebook crawler). But I can't even get those rules to work. I am debugging using the Facebook OG debugger
<rule name="Rule1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_USER_AGENT}" pattern="facebookexternalhit/1.1|Facebot" />
</conditions>
<action type="Redirect" url="new url here" />
</rule>
Not an answer but a starting point. The IIS Manager (IIS 8 on Windows 8.1) translates your apache mod_rewrite rules into this slightly different configuration:
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="qs/(\d*)$" ignoreCase="false" />
<conditions>
<add input="%{HTTP_USER_AGENT}" pattern="(facebookexternalhit/[09]|Twitterbot|Pinterest|Google.*snippet)" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="http://sitetocrawl.com/doc?id={R:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I see that it is rewrite instead of redirect, but please check if this would work for your scenario. And if it works, you may begin changing it until reaching desired result.
And now I see that Your main URL Matching pattern is simply urlmatchpattern which of course is not a pattern and is the root cause for your rules to not work.
I'm trying to use the rewrite rule from the HTML 5 Boilerplate project to make circumventing browser cache (aka cache busting):
<rewrite>
<rules>
<rule name="Cachebusting">
<match url="^(.+)\.\d+(\.(js|css|png|jpg|gif)$)" />
<action type="Rewrite" url="{R:1}{R:2}" />
</rule>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://chewsy.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If i try to access my css with /css/all.123456.css, it fails to find the file with the error reporting that it's looking for /css/all.123456.css (no rewrite). I've tried commenting out the "Remove WWW" rule to see if that was a conflict, but same behavior.
Any ideas why this rule isn't being applied and rewriting the URLs?
Update: I'm using these settings for my web server in VS2010:
<match url="^(.+)\.\d+\.(js|css|png|jpg|gif)$" />
<action type="Rewrite" url="{R:1}.{R:2}" />
I pressume you want to get /css/all.css, if not, post the desired result...
EDIT: VS internal development server (Cassini) doesn't support IIS URL Rewriting Module, you would have to use IIS (Express) for that, or some third party component (http://urlrewriter.net/)...
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.