I have the following rewrite rule in web.config
<rewrite url="~/product/(.+)" to="~/outbound-link-proxy.aspx?product=$1" />
This should rewrite (e.g.)
mydomain.com/product/car
to
mydomain.com/outbound-link-proxy.aspx?product=car
This is working fine. However I have noticed that some sites are linking to mine and adding their own query string on the end. I don't mind this but it is breaking my rewrite.
mydomain.com/product/car?foreignQueryString=983249
How can I create a rewrite rule to preserve my original rewrite but disregard any query strings so that
mydomain.com/product/car?foreignQueryString=983249
still rewrites to
mydomain.com/outbound-link-proxy.aspx?product=car
Here's the answer
<rewrite url="~/product/(.+)\?(.+)" to="~/outbound-link-proxy.aspx?product=$1" />
A \? to match a literal question mark then (.+) to match number of any other characters to end of string.
Related
Is it possible to rewrite/redirect a url such as this:
http://example.com?id=123&id=123
To instead be:
http://example.com?id=123
The actual reason for doing this is a bug in code, but a URL rewrite/redirect would be a quick workaround.
Didn't realize it was possible to add a condition for {QUERY_STRING} that matches the pattern id=(.*)&id=.* and then just redirect to http://{HTTP_HOST}/{R:0}?id={C:1}
I have recently found out that, if there are multiple URLs (eligible for rewriting) in one line of a web page, the IIS / ARR would only rewrite the first match of that line, and ignore the rest. So I'd like to ask two questions:
Is this the default behavior of the IIS / ARR URL rewriting function?
Is there any work-around for this behavior, such that the IIS / ARR could recognize -- and rewrite -- multiple URLs on the same line?
The solution for me was to ensure Regex doesn't match anything outside of the quotes, i.e. stop it from being greedy.
Using the GUI the match pattern ends with ([^"]*) instead of (.*), and when saved in the Web.config this gets escaped to ([^"]*)
The line in my Web.config looks like this:
<match filterByTags="None" pattern="http://your.domain/~/media([^"]*)" />
So I think it's safe to assume you're referring to an outbound rewrite rule to change URLs in HTML responses.
No that is not the default behaviour of the rewrite module.
Update your question to include your rule configuration so that we can help.
Useful information:
In your <match> element if you do not specify the filterByTags, then the match pattern will be applied on the entire response content, regardless of lines and occurrences. Note that the evaluation of regular expression patterns on the entire response content is a CPU intensive operation and may affect the performance of the web application.
It sounds to be that your rule(s) aren't properly configured.
Further information:
URL Rewrite Module 2.0 Configuration Reference
Creating Outbound Rules for URL Rewrite Module
I use IIS Rewrite Module. I have a few static files, and they are stored at
website/static/xyz/abc
I added IIS Rewrite rule as follows:
regular expression pattern:
xyz/.+
rewrite URL:
/static/{R:0}
First, if I type (with an ending /)
http://mysite/xyz/abc/
everything seems working just fine. The browser address bar shows exactly what I typed.
Second, If I type (without the ending /)
http://mysite/xyz/abc
the website still displays the content correctly, but the browser address bar changes to (by adding static and the ending /)
http://mysite/static/xyz/abc/
How can I make IIS not show /static in the second situation?
Thanks and regards.
UPDATE
Regular expression is used.
<rule name="inauguralball">
<match url="inauguralball(/.*)?" />
<action type="Rewrite" url="/static/{R:0}" />
</rule>
I think I found your answer here: http://support.microsoft.com/kb/298408/EN-US. When you leave out the trailing slash and IIS finds a directory that matches your request, it performs a redirect for you. I believe this is where your redirect is coming from, thereby sending you to /static/inauguralball/. Notice that IIS has also added the trailing slash for you because it detected that directory exists.
Can you make sure that your hrefs always point to /inauguralball/ (with the trailing slash) so that IIS doesn't perform that automatic redirect for you?
I am looking and looking and coming up with all different patterns, but no such luck. I require a rewrite rule to handle the a replacement of a string xyzlisting with xyz-listing. It may be anywhere in the url. Is there a straightforward way to write this rule?
I am not so experienced with IIS. However, I can suggest something:
In Apache mod_rewrite, you can use this rule:
RewriteRule (.*)(/\w+)(listing)(.*)$ $1$2-$3$4
This rule will split whatever string in the format anything followed by 'listing' that comes after mydomain.com/ with a '-'. Which results in 'anything-listing', wherever in the URL.
This will only change the last occurrence of this type of string. It will not replace more than one occurrence.
You can import this URl rewrite rule from Apache to IIS using this IIS extension:
IIS Extenson page for url rewrite module
Hope this helps :)
I am in the process of moving Blog from dasBlog to BlogEngine. In dasBlog, my URLs looks like this.
http://pfsolutions-mi.com/blog/2008/03/08/Beyond-Compare.aspx
Whereas in BlogEngine, my URLs looks like this.
http://pfsolutions-mi.com/blog/post/2008/03/08/Beyond-Compare.aspx
The only difference between the two URLs is the "post" sub-folder in BlogEngine.
Since I'm currently using IIS URL Rewrite to remove WWW from the URL, I figured the easiest solution would be to create another rule to handle adding the sub-folder. I tried something like this.
rule name="Blog Redirect" enabled="true" stopProcessing="true"
match url="^blog/([_0-9]+)/([_0-9]+)/([_0-9]+)/([_0-9a-z-]+).([_0-9a-z-]+)$"
action type="Redirect" url="blog/post/{R:1}/{R:2}/{R:3}/{R:4}.{R:5}" redirectType="Temporary"
However, when I enter an old dasBlog URL it does not get redirected to the new location. Instead I get the generic BlogEngine 404 Error page.
Note: I plan to change the redirectType to Permanent once I know everything is working.
Shouldn't your matching regex look more like this?
match url="^blog/([0-9]+)/([0-9]+)/([0-9]+)/([\w-]+)\.([a-z]+)$"
There are no underlines in date numbers anyway and your [_0-9a-z-]+ doesn't include uppercase letters like in "Beyond-Compare".
So here we should have: url="^blog/digits/digits/digits/any-word-characters.lowercase-letters$"
We could also specify more with:
match url="^blog/([0-9]{2,4})/([0-9]{2})/([0-9]{2})/([\w-]+)\.([a-z]{3,4})$"
Based on the assumption that you always have:
Year as "08" or "2008"
Month and day as "01" or "11"
File endings with 3 or 4 lowercase letters (htm, html, php, asp, aspx, etc.)
EDIT: I think "\w+" does not include hyphen, so you must turn this into "[\w-]+"