I can't figure out why a certain rewrite rule does not work.
I have this rule:
<rule name="CanonicalHostNameRule1" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="testsite\.com" negate="true" />
</conditions>
<action type="Redirect" url="https://www.testsite.com/{R:1}" />
</rule>
When I go into IIS and using the URL Rewrite module, I tested using "http://testsite.com". This works. But after placing an updated web.config in production, these are my results from typing in these into the url:
I was able to reach the site by typing in these variations:
https://www.testsite.com
http://www.testsite.com
www.testsite.com
testsite.com
But I received the error message "testsite.com refused to connect" when typing in:
https://testsite.com
http://testsite.com
What am I missing, that is preventing the last 2 from working? It seems like the rewrite rule I have should catch all requests.
Thank you in advance!
My guess would be that for the last two examples, their {HTTP_HOST} will have no www which means that it will match your condition resulting in your negate attribute skipping your rule action.
I would recommend enabling IIS Failed Request Tracing in order to identify exactly why your rewrite rule isn't working for those values.
Related
I am working on an existing project that I did not create.
In the web.config file, there are a number of redirects, setup in the following way:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/findyourlocalbranch/" value="/find-your-local-branch/" />
</rewriteMap>
</rewriteMaps>
It seems to me that a rewrite rule is being used to do these redirects, but as I said, I did not write this and am not in a position to change this. I am not sure if this is right or wrong.
My problem is that I need to implement the following redirect:
<add key="/need-a-loan/.htm" value="/need-a-loan/" />
but this brings the whole site down - I suspect due to a syntax error in this line.
I suspect the error is the '.' after the '/'
My question is, what can I do about it - there is nothing on the internet that I can find about this.
Basically, I want to redirect this one, specific URL to the given URL???
I can't say whats going on with the map, as without the corresponding rule I'm blind.
But, this rewrite rule will redirect from /need-a-loan/.htm to /need-a-loan/.
<rewrite>
<rules>
<rule name="Need-A-Loan" stopProcessing="true">
<match url="need-a-loan/.htm" />
<action type="Redirect" url="need-a-loan/" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Note: doesn't use the map, so you'd remove that entry.
That will give you a 301. If you want a different type, you can adjust the rediectType. E.g. Temporary will give you a 307.
OK, I'm wondering if someone can lend a hand with a regex I'm trying to write.
Basically, what I want to do is use IIS urlrewrite module to do a redirect to a specific URL if the user accesses a URL on another site. The only catch is I have to also capture a bit of the query string, and move it into the redirect.
so here is the input, the URL that a user may access would look like:
https://of.example.com/sfsv3.aspx?waform=pro&language=en
I want to match that URL (either http or https, case insensitive), and capture from it also one piece of information, the two letter language code. then the url i want to forward the user to looks like:
http://example.com/ca/en/ppf
(where en is replaced by whatever i captured above)
So, I'm working with IIS Rewrite module, and I've gotten my input data and regex in, so far the regex pattern I have is this:
https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))
so basically i'm matching the whole string, plus a group and a subgroup for language and it's code. in the IIS test pattern dialog, this is working.
I get the following
{R:1} language=en
{R:2} en
great! so then my IIS rewrite rule should look like this to redirect the user:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true" enabled="true">
<match url="https?://of.example.com/sfsv3.aspx\?waform=pro&(language=(..))" ignoreCase="true" />
<action type="Redirect" url="http://www.example.com/ca/{R:2}/ppf" />
</rule>
</rules>
</rewrite>
</system.webServer>
this all seems right to me. however, the redirect is not occurring. it seems to have a problem with the part \? (an escaped question mark to mark the start of the query string). if this is included, then the redirect simply does not happen.
Can you help me figure out how to get it work?
for the record, I figured it out. this is a special case of regex, running it inside a web.config as part of a urlrewrite action. in that case, you can't handle the query string with simple regex, you have to put in conditions on the query string. Here's what eventually ended up working:
<system.webServer>
<rewrite>
<rules>
<rule name="redirect" stopProcessing="true" stopProcessing="true" enabled="true">
<match url="sfsv3.aspx\.aspx" ignoreCase="true"/>
<conditions>
<add input="{QUERY_STRING}" pattern="subject=PROPRCH" />
<add input="{QUERY_STRING}" pattern="(..)/subject" />
</conditions>
<action type="Redirect" url="https://www.example.com/ca/{C:1}/forms/ppf" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)
I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). Using the GUI provided by IIS I created the following rule:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="site1/(.*)" />
<action type="Rewrite" url="http://localhost:7001/{R:1}" />
</rule>
</rules>
</rewrite>
Quiet simple, but unfortunately it does not work. On the other hand, when I change the action type to Redirect, it works fine.
What could be the problem?
I ran into this same issue yesterday, and it took me a long time to figure out.
The key here is that you've got an http:// prefix in your rewrite action; that makes this a special case that needs to be handled by Application Request Routing. The first step is to make sure that the Application Request Routing module is installed. You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing. Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. From the actions on the right, choose Server.Proxy.Settings, and make sure that the "Enable Proxy" checkbox is checked. This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests.
The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
Stumbled across this old post when I was trying to solve the same issue.
SOLVED!
Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule.
This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite.
I edited this to suit 1 match. Then I just duplicated this code editing the product ID for each. Example below:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^product\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
</conditions>
<action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator"
appendQueryString="false" />
</rule>
The first rule looks for the string "product.php" in the URL and "id_product=35", it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. Then (see below)
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="product.php?id_product=35" />
</rule>
This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect.
Do make sure MVC routing doesn't steal your request. To prevent that from happening, ignore the route you're trying to rewrite:
RouteTable.Routes.Ignore("blog/{*pathInfo}");
Inspired by: https://sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore
Change the Rewrite URL to AbsolutePath instead putting http://...
it should be
<action type="Rewrite" url="{R:1}" />
It worked for me, but in my case, I have been rewrite to a fixed webpage.
I'm wondering if it is possible to have an IIS7 URL rewrite rule that does something like this. When someone goes to:
http://www.mydomain.com/images/0000022_0-Hello_World.jpeg
it will request the file at:
http://www.mydomain.com/images/0000022_0.jpeg
I tried to make a rule to match (.)/images/(.)-(.*).jpeg and then rewrite it to {R:1}/images/{R:2}.jpeg, but it doesn't work. I just get a 404 error. I also tried adding the ISAPI Wildcard, but it didn't change anything.
I'm using .net 4.0
Thanks!
Assuming you use IIS Manager ( like in this article):
Try to create a rewrite rule which matches the following pattern (regular expressions):
^images/([0-9_]+)-.*\.jpeg$
As Action, choose rewrite, and fill in the following rewrite Url:
images/{R:1}.jpeg
No other rule should be executed after that (-> Stop processing of subsequent rules checkbox)
You don't need to add the ISAPI Wildcard for this as you have an extension (.jpeg) so should be handled by the StaticFile Handler.
You should put this after other rewrite rules ,for virtual folders etc..
<rule name="rule 1s" stopProcessing="true">
<match url="\.(gif|jpe?g|png|bmp)" ignoreCase="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="//ressources/default.png" />
</rule>