URL Rewriting with Query Strings using IIS URL Rewrite Module - iis-7

I am trying to rewrite a url such as:
new_membership?R=1&I=2
to:
agent_new_membership.aspx?R=1&I=2
but I am getting an error reading: "HTTP Error 404.0 - Not Found"
This is the web.config code I am using:
<rewrite>
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/administration" value="/administration_main.aspx" />
<add key="/change_admin_password" value="/admin_change_password.aspx" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="New Membership" stopProcessing="true">
<match url="^new_membership?R=(\d+)&I=(\d+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="agent_new_membership.aspx?R={R:1}&I={R:2}" />
</rule>
<rule name="Redirect rule1 for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
What am I doing wrong?
Thanks in advance.

This should work for you:
<rule name="TestRule" stopProcessing="true">
<match url="^new_membership\?R=(\d+)&I=(\d+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="agent_new_membership.aspx?R={R:1}&I={R:2}" />
</rule>

Related

Force redirect to HTTPS

I need to force redirect to HTTPS in the following Trailing Slash RULE.
<rule name="Trailing Slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{URL}" pattern="WebResource.axd" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>
I need the end result to be of something below:
<rule name="Trailing Slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{URL}" pattern="WebResource.axd" negate="true" />
</conditions>
<action type="Redirect" url="https://www.somedomain.com/{R:1}/" redirectType="Permanent" />
</rule>

How FIX choosing your permalink structure on WordPress for IIS server on Azure

Need your help.
There is one IIS server in Azure.
It contains the Website and the Angular SPA application.
I need to install in a separate directory WordPress blog.
Blog unfolded.
But there was a problem with Choosing your permalink structure.
I have an error:
403 - Forbidden: Access is denied.
Credentials that you are supplied with.
I have two web.config files
1. ROOT web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path=".">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
</security>
<rewrite>
<rules>
<clear />
<rule name="Redirect http to https" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
</rule>
<rule name="login redirect" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^login" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="ANGULAR-SPA-DIR/login" />
</rule>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="/ROOT-DIR/" ignoreCase="true" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="/ANGULAR-SPA-DIR/" ignoreCase="true" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="/TEST/" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
<rule name="AngularJS Routes1" stopProcessing="true">
<match url="^ANGULAR-SPA-DIR/" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/ANGULAR-SPA-DIR/index.html" />
</rule>
<rule name="AngularJS Routes2" stopProcessing="true">
<match url="^TEST/" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/TEST/index.html" />
</rule>
<rule name="WordPress Blog" stopProcessing="true">
<match url="blog/.*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
</conditions>
<action type="None" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
<caching>
<profiles>
<add extension=".css" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="00:30:00" />
<add extension=".svg" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="01:00:00" />
<add extension=".jpg" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="01:00:00" />
<add extension=".js" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="00:30:00" />
<add extension=".woff2" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="24.00:00:00" />
</profiles>
</caching>
<directoryBrowse enabled="true" />
</system.webServer>
</location>
</configuration>
Blog web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules><remove name="WordPress Blog"/>
<rule name="WordPress: https://ROOT-URL/blog" patternSyntax="Wildcard">
<match url="*"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
</conditions>
<action type="Rewrite" url="index.php"/>
</rule></rules>
</rewrite>
</system.webServer>
</configuration>
What can suggest experienced friends?
Add srting to ROOT web.config
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
...
<add input="{QUERY_STRING}" pattern="/blog/" />
</conditions>
After that, they "Choosing your permalink structure" work correctly

Rename product page with web.config

Since I have this URL:
http://namedomain.com/product.asp?id=01&color=05&name=xxxx
What is the better approach to rewrite this URL like this:
http://namedomain.com/xxxx
With IIS I was able to generate this URL
http://namedomain.com/01/05/xxxx
This is the web.config code:
<rule name="RedirecionaPagProduto" stopProcessing="true">
<match url="^exibe_produtos\.asp$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^produto=([^=&]+)&cor=([^=&]+)&url=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<rule name="RenamePagProduto" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="exibe_produtos.asp?produto={R:1}&cor={R:2}&url={R:2}" />
</rule>
Anyone can help?

IIS URL Rewrite working in one page only

I have defined a URL Rewrite rule through IIS. Basically it turns something like this:
Article.aspx?ID=1&FriendlyURL=whatever
INTO
/1/whatever
Please note that Redirection is working right, but URL Rewrite (links within the page) are not being translated unless I am inside the Article.aspx page.
How can I make the Rewrite Rule apply to all the pages instead of only one? I'm posting below the written rules from Web.Config for your reference. Thanks.
<system.webServer>
<rewrite>
<outboundRules>
<rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*/)Article\.aspx\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rewriteMaps>
<rewriteMap name="Article Rewrite">
<add key="Article.aspx?ID=1&FriendlyURL=whatever" value="/1/whatever" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Article\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^ID=([^=&]+)&FriendlyURL=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Article.aspx?ID={R:1}&FriendlyURL={R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
So I finally had to hard-code the links to be url-friendly by setting the "href" attribute within the code.
Something like this:
<a href='/1/hello-world/'>Read the "Hello World" Article</a>
Thanks.
I like regular expressions problems, try this.
<system.webServer>
<rewrite>
<outboundRules>
<clear />
<rule name="OutboundRewriteUserFriendlyURL1"
preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img"
pattern="^(.*/)([^\.]+)\.aspx\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="true" />
<action type="Rewrite"
value="{R:1}{R:2}/{R:3}/{R:4}/" />
</rule>
<rule name="OutboundRewriteUserFriendlyURL2"
preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img"
pattern="^(.*)\?ID=([^=&]+)&(?:amp;)?FriendlyURL=([^=&]+)$" />
<conditions logicalGrouping="MatchAll"
trackAllCaptures="true" />
<action type="Rewrite"
value="" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}"
pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rewriteMaps>
<rewriteMap name="Article Rewrite">
<add key="Article.aspx?ID=1&FriendlyURL=whatever"
value="/1/whatever" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="RedirectUserFriendlyURL1"
stopProcessing="true">
<match url="^([^\.]+)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}"
pattern="^POST$"
negate="true" />
<add input="{QUERY_STRING}"
pattern="^ID=([^=&]+)&FriendlyURL=([^=&]+)$" />
</conditions>
<action type="Redirect"
url="{R:1}/{C:1}/{C:2}"
appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1"
stopProcessing="true">
<match url="^([^/]+)/([^/]+)/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}"
matchType="IsFile"
negate="true" />
<add input="{REQUEST_FILENAME}"
matchType="IsDirectory"
negate="true" />
</conditions>
<action type="Rewrite"
url="{R:1}.aspx?ID={R:2}&FriendlyURL={R:3}" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="false"
doDynamicCompression="false" />
</system.webServer>
The problem is in your OutboundRewrite rule's regular expression. I suggest you get a regex tool like expresso (my favorite), start with a very simple regex, then add complexity as your situation dictates.
The simplest regex that will match your example is:
Article\.aspx\?ID=(\d)&FriendlyURL=(.*)
Here's an example. Godspeed.

ASP.NET Web.config issue with MagicAjax and Rewrite Rules

Hey, I have the following rules in webconfig for my ASP.NET website which also contains several sections which are controlled in wordpress.
<rules>
<rule name="RedirectFinancial" stopProcessing="true">
<match url="^orgprofile/financial\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^name=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/financial" appendQueryString="false" />
</rule>
<rule name="RewriteFinancial" stopProcessing="true">
<match url="^([^/]+)/financial/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="orgprofile/financial.aspx?name={R:1}" />
</rule>
<rule name="RedirectGovernance" stopProcessing="true">
<match url="^orgprofile/governance\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/governance" appendQueryString="false" />
</rule>
<rule name="RewriteGovernance" stopProcessing="true">
<match url="^([^/]+)/governance/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="orgprofile/governance.aspx?name={R:1}" />
</rule>
<rule name="RedirectOverview" stopProcessing="true">
<match url="^orgprofile/overv\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^name=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteOverview" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="orgprofile/overv.aspx?name={R:1}" />
</rule>
<rule name="RedirectPurpose" stopProcessing="true">
<match url="^orgprofile/purpose\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^name=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/purpose" appendQueryString="false" />
</rule>
<rule name="RewritePurpose" stopProcessing="true">
<match url="^([^/]+)/purpose/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="orgprofile/purpose.aspx?name={R:1}" />
</rule>
<rule name="RedirectActivities" stopProcessing="true">
<match url="^orgprofile/activities\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^name=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}/activities" appendQueryString="false" />
</rule>
<rule name="RewriteActivities" stopProcessing="true">
<match url="^([^/]+)/activities/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="orgprofile/activities.aspx?name={R:1}" />
</rule>
<rule name="RewriteWordPress" patternSyntax="ECMAScript">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="\.axd$" negate="true" />
<add input="{URL}" pattern="view.aspx$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="^([^/]+)/financial/?$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="^([^/]+)/governance/?$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="^([^/]+)/?$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="^([^/]+)/purpose/?$" negate="true" />
<add input="{REQUEST_FILENAME}" pattern="^([^/]+)/activities/?$" negate="true" />
</conditions>
<serverVariables />
<action type="Rewrite" url="index.php" />
</rule>
The problem with magicajax when I add the above Rules is that my magicajax uploader doesnt work as intended anymore and when I click on the upload button it causes a page refresh where as before it would come up automatic.
Any Ideas how I can get around this ?
EDIT : The problem seems to be overview rewrite rule
My friendly urls look like this
http://hostname/foldername/companyname <--- overview
http://hostname/foldername/companyname/purpose <---- i.e purpose rewrite rule

Resources