Url rewriting not working as expected - wcf

I am trying to rewrite the rule so that if no .svc comes in URL it should rewrite and append .svc in URL.
These are my rules:
<rewrite>
<rules>
<rule name="RemoveSVC" stopProcessing="true">
<match url="localhost/RestApp/Services/([a-zA-Z0-9]*)/(.*)$" />
<action type="Rewrite" url="localhost/RestApp/Services/{R:1}.svc/{R:2}"
appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
Can anybody tell me why is this rule not working?

I think you want:
<rewrite>
<rules>
<rule name="RemoveSVC" stopProcessing="true">
<match url="localhost/RestApp/Services/([a-zA-Z0-9]*)/(.*)" />
<action type="Rewrite" url="localhost/RestApp/Services/{R:1}.svc/{R:2}"
appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
Remove the $ sign on the end.
I'd also get rid of the localhost/ part because that would only work on your local dev PC, when you deploy into production it won't be localhost any more:
<match url="RestApp/Services/([a-zA-Z0-9]*)/(.*)" />
<action type="Rewrite" url="RestApp/Services/{R:1}.svc/{R:2}"
appendQueryString="true" logRewrittenUrl="true" />

Related

How create condition for url rewrite?

I have similar urls. But I don`t know how write one rule for my situation.
<rule name="stage">
<match url="stage.some.com/test/docs/overview.htm" />
<action type="Redirect" url="https://stage-docs.some.com/" appendQueryString="false" redirectType="Permanent" />
</rule>
<rule name="prod">
<match url="some.com/test/docs/overview.htm" />
<action type="Redirect" url="https://docs.some.com/" appendQueryString="false" redirectType="Permanent" />
</rule>
First rule has in domain stage.
Any help would be greatly appreciated.

How add exception in IIS url rewrite (ASP NET Core)?

I have rule:
<rule name="main" stopProcessing="true">
<match url="main/([^.]*)/$" />
<action type="Redirect" url="/main/" appendQueryString="false" redirectType="Permanent" />
</rule>
References /main/xxxx/ go to /main/, but I need make rule with one reference (/main/docs/) whitch doesn`t have redirect.
Help me please make exception in rule
References /main/xxxx/ go to /main/, but I need make rule with one reference (/main/docs/) whitch doesn`t have redirect.
To achieve the requirement, you can try to add a condition to the rule, like below.
<rule name="main" stopProcessing="true">
<match url="main/([^.]*)/$" />
<conditions>
<add input="{REQUEST_URI}" pattern="main/doc/" negate="true" />
</conditions>
<action type="Redirect" url="/main/" appendQueryString="false" redirectType="Permanent" />
</rule>

URL Rewriting in IIS with Express

I want to rewrite all images to a different folder. I'm using IIS and have configured a rule in the web.config to redirect all requests to a node.js file as follows:
<rewrite>
<rules>
<rule name="img">
<match url="\/(.*).img" />
<action type="Rewrite" url="/handlers/img.js" />
</rule>
</rules>
</rewrite>
All requests are now being sent to the img.js file, where based on a condition, I want to redirect to another image file. But IIS now sends that file to the img.js and it ends up as a loop. Is there any way out of this loop?
You could try the below thing to resolve the issue:
set the condition to do not match the pattern:
<conditions>
<add input="{REQUEST_URI}" pattern="\/(.*).img.js" negate="true" />
</conditions>
or set <rule name="img" stopProcessing="true">
<rule name="img" stopProcessing="true">
<match url="\/(.*).img" />
<conditions>
<add input="{REQUEST_URI}" pattern="\/(.*).img.js" negate="true" />
</conditions>
<serverVariables />
<action type="Rewrite" url="/handlers/img.js" logRewrittenUrl="true" />
</rule>

IIS 10 (server 2016) SSL redirect to www. and .se

I have a site with multiple domainnames and have just implemented an ssl certificate to the site. but there is a problem when redirecting the incomming requests.
I want to configure it so all requests should redirect to one singel domainname (https://www.barnensbibliotek.se).
The ssl cert is for *.barnensbibliotek.se and www.barnensbibliotek.se and the other domainnames should just redirect to them regardless if the are http or https
i would like to redirect all other domainnames for barnensbibliotek like: barnensbibliotek.se, barnensbibliotek.com and www.barnensbibliotek.com (.info, .org, .nu, .net) to the https://www.barnensbibliotek.se.
I used this code in the web.config (this code works for www.barnensbibliotek.se but not for the others)
<rewrite>
<rules>
<rule name="Redirect to https" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Is there a way to solve this?
This problem was solved widt this code:
<rewrite>
<rules>
<rule name="http: to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>

IIS URL Rewrite

I have the following rewrite rule:
<rewrite>
<rules>
<rule name="FrontController" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="wcf/api.svc/auth/home" />
</rule>
</rules>
</rewrite>
This basically rewrites all non-file urls to web service api calls that returns index.html in an SPA backed by WCF.
The above rewrite ends up including all the query string parameters that were included with the original URL. What I need to do is also include the original URL, such as, 'wcf/api.svc/auth/products', as a query string parameter in the rewritten URL, such as 'https://domain.com/wcf/api.svc/auth/products?enc=lkjewro8xlkz' being transformed into 'https://domain.com/wcf/api.svc/auth/home?enc=lkjewro8xlkz&orig=wcf/api.svc/auth/products'.
Is this possible, and if so, what changes would I need to make to achieve it? I would like for my WCF application to know about the original URL so that it can configure the SPA to initialize to a particular view on load.
Thanks
It's quite possible.
You need to add the URL Encoded value of the {REQUEST_URI} to the Rewrite URL.
<rewrite>
<rules>
<rule name="FrontController" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="wcf/api.svc/auth/home?orig={UrlEncode:{REQUEST_URI}}" />
</rule>
</rules>
</rewrite>
With this rule, in your WCF endpoint orig parameter would be:
/wcf/api.svc/auth/products?enc=lkjewro8xlkz
If you don't want the query string part (?enc=lkjewro8xlkz), you'll need an extra condition to match the URI without query string.
<rewrite>
<rules>
<rule name="FrontController" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<!-- match any character up to a question mark -->
<add input="{REQUEST_URI}" pattern="^[^\?]+" />
</conditions>
<!-- {C:0} means the first match in conditions -->
<action type="Rewrite" url="wcf/api.svc/auth/home?orig={UrlEncode:{C:0}}" />
</rule>
</rules>
</rewrite>
Now, orig will be /wcf/api.svc/auth/products in the WCF endpoint.
Hope it helps.