URL Rewrite 500.52: Unrecognized element 'serverVariables' - iis-7

I need to implement a reverse proxy using only my web.config and I'm having trouble with the 500.52 error. As suggested in many places online, I added the serverVariables section to include the HTTP_ACCEPT_ENCODING variable, but I am met with an error:
Config Error Unrecognized element 'serverVariables'
Below is my web.config.
<rewrite>
<allowedServerVariables>
<add name="HTTP_ACCEPT_ENCODING" />
</allowedServerVariables>
<outboundRules>
<rule name="reverseProxy">
<match pattern="http://linkInCode.com"></match>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" value="https://linkIWant.com/script.js"></action>
</rule>
</outboundRules>
</rewrite>
What is the problem? ServerVariables should be a recognised element.

You need to have the server variable registered previously in <allowedServerVariables>,
https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-20-configuration-reference#Allowed_Server_Variables_List

Using this blog post, I copied the examples given and changed sections for my needs.
<rewrite>
<rules>
<rule name="Rewrite to article.aspx">
<match url="^article/([0-9]+)/([_0-9a-z-]+)" />
<action type="Rewrite" url="https://URLtoHide.com" />
</rule>
</rules>
<outboundRules>
<rule name="Rewrite to clean URL" preCondition="IsHTML">
<match filterByTags="A" pattern="http://http://URLtoShow.com" />
<action type="Rewrite" value="https://URLtoHide.com" />
</rule>
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>

Related

Can I configure IIS to route traffic to another server?

I have a Windows server available at website.com, and I have another server with a WordPress on it.
How can I configure IIS to route website.com/blog to my WordPress?
I tried to use virtual directories, but I can only send my user to the same server.
According to your description, I suggest you could consider using ARR reverse proxy to achieve your requirement.
I suggest you could install the ARR by using this link and this link.
Then I suggest you could try to use below url rewrite rule.
<rule name="ReverseProxyInboundRule2" stopProcessing="true">
<match url="/blog/(.*)" />
<action type="Rewrite" url="http://ec2 ubuntu address/{R:1}" />
</rule>
Thank you,it worked but I had to add more code to my web.config.
My web.config:
<rewrite>
<rules>
<clear />
<rule name="Route the requests for Company1" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="https://blog.raczum.com/{R:0}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
<rule name="Route the subfolder blog" enabled="false" patternSyntax="Wildcard" stopProcessing="false">
<match url="https://raczum.com/blog/*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="https://blog.raczum.com/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule> </rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true" stopProcessing="false">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.raczum.com/(.*)" />
<action type="Rewrite" value="/blog/{R:2}" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
</rule>
<rule name="RewriteRelativePaths" preCondition="ResponseIsHtml1" enabled="true" stopProcessing="false">
<match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="^http(s)?://blog.raczum.com/(.*)$" negate="false" />
<action type="Rewrite" value="/blog/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1" patternSyntax="Wildcard">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="*" />
</preCondition>
<preCondition name="ResponseIsHtml2">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>

IIS Rewrite Rule for non exsting directory to root for a specific page

Here is the situation:
www.mydomain.com/anydirectory/specificwebpage should go to www.mydomain.com/specificwebpage without changing the url in the browser.
Here is what i have tried so far.
<rewrite>
<rewriteMaps>
<rewriteMap name="dir">
<add key=".*/test.cfm" value="test.cfm" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for dir" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{dir:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true"
/>
</rule>
</rules>
</rewrite>
Created a rewrite map for the specific webpage and used a wildcard for the non-existent directory.
/(.*)/test.test -----> /test.test
Thanking you in advance for your help guys.
Re

Wordpress behind ARR proxy - JSON request not signed

Using IIS ARR I have a reverse proxy to a WP blog hosted elsewhere on a Linux server.
These are the proxy rules:
<rewrite>
<rules>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/blog/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Reverse proxy to blog" stopProcessing="true">
<match url="^(.*)" />
<serverVariables>
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
<action type="Rewrite" url="http://111.22.33.444/blog/{R:1}" />
</rule>
</rules>
<outboundRules>
<rule name="Add application prefix" preCondition="IsHTML" stopProcessing="true">
<match filterByTags="A, Form, Img, Link, Script" pattern="^http://111.22.33.444/blog/(.*)" />
<action type="Rewrite" value="https://www.example.com/blog/{R:1}" />
</rule>
<rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
<match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
<action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
</rule>
<preConditions>
<preCondition name="NeedsRestoringAcceptEncoding">
<add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
</preCondition>
<preCondition name="IsHTML">
<add input="{HTTP_HOST}" pattern="111.22.33.444" />
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
<add input="{URL}" negate="true" pattern="wp-admin/" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
"wp-admin/" has been excluded from the IsHTML pre-condition as this was causing redirect loops in some areas of the admin.
The plugin Jetpack has been installed but this is failing to connect to Wordpress.com. Wordpress.com shows the error "There was an error retrieving your site settings." Looking at the errors in Plesk where the blog is actually hosted we see the URL below failed with a 400 bad request.
blog/?rest_route=%2Fjetpack.................
If this URL is called manually in a web browser, it gives the message "The request is not signed correctly".
Can anyone offer any advise on resolving this bad request issue please?

url rewriting in iis7

I am running IIS7.5 and the URL Rewrite module.
I followed these step by step instructions to enable user friendly URL's:
Rule Templates for the URL Rewrite Module - User Friendly URL - rule template
If I enter the URL http://domain.com/default/ instead of http://domain.com/default.aspx the website throws 404: file not found error.
Here are my rewrite rules:
<rewrite>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^demourl\.dev\.asenetechdev1\.com/default\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="demourl.dev.asenetechdev1.com/default"
appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^demourl\.dev\.asenetechdev1\.com/default$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="false" />
</conditions>
<action type="Rewrite" url="demourl.dev.asenetechdev1.com/default.aspx" />
</rule>
</rules>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img"
pattern="^(.*)demourl\.dev\.asenetechdev1\.com/default\.aspx$" />
<action type="Rewrite" value="{R:1}/ demourl.dev.asenetechdev1.com/default" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
I also tried using Intelligencia.UrlRewriter with this. But I am able to generate friendly urls on local as well as on server, but I want extensionless url, which is working ok on my local server, but not on the live server.
Your pattern is currently not matching /default/. It is only matching /default
Changing
<match url="^demourl\.dev\.asenetechdev1\.com/default$" />
to
<match url="^demourl\.dev\.asenetechdev1\.com/default/{0,1}$" />
should do it.

Conditional query string redirect

I'm trying to redirect requests that have a query string to a different domain name.
I've got a short url, http://short.url and I want to redirect http://short.url?hello to http://long.url/?hello. So the query string has to be kept by the redirect. To make things more complicated, I would like to rediect http://short.url?hello,hello2 to http://long.url/advanced.aspx/?hello,hello2.
Here is the rule I've got now (only dealing with the first part of my question)
<rewrite>
<rules>
<rule name="test" patternSyntax="ECMAScript" stopProcessing="true">
<match url="~/?\w+" />
<action type="Redirect" url="http://long.url/?{R:0}" redirectType="Found" />
</rule>
</rules>
</rewrite>
However, I am not seeing any redirects. Also, is there a better way to do this? Basically I just want to setup a shortcut to pass queries to a website. These are not meant to be permanent so I'm using redirectType="Found".
In case anyone is looking to do this:
<rules>
<rule name="Basic" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="\w+" />
<add input="{QUERY_STRING}" pattern="\," negate="true" />
</conditions>
</rule>
<rule name="Advanced" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\w*" />
<action type="Redirect" url="http://longurl.com/advanced.aspx" redirectType="Found" />
<conditions>
<add input="{QUERY_STRING}" pattern="^\w+(?=\,)" />
</conditions>
</rule>
</rules>

Resources