I have the following two rules for my WCF:
<system.webServer>
<rewrite>
<rules>
<!-- Rewrite requests to /MyService to /MyService.svc -->
<rule name="MyService" stopProcessing="false">
<match url="MyService/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>
<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
<match url=".*(MoreServices)(.+)" ignoreCase="true" />
<action type="Rewrite" url="{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>
The first rule:
<!-- Rewrite requests to /MyService to /MyService.svc -->
<rule name="MyService" stopProcessing="false">
<match url="MyService/(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="/MyService.svc/{R:1}" />
</rule>
Rewrites calls that get sent without the svc extension to the svc service.
The second rule:
<!-- Remove path /MoreServices from url -->
<rule name="example" stopProcessing="true">
<match url=".*(MoreServices)(.+)" ignoreCase="true" />
<action type="Rewrite" url="{R:2}" />
</rule>
Removes an extra path and directs to the correct service.
Everything works fine, however, the site I will be publishing to does not allow the <rule> tag to be used in the web.config. My question is, how can I modify my WCF to accomplish the two rules above programmatically. Basically duplicating the logic of the two items mentioned above in C# as part of the WCF service. Thank You.
I found the solution!
Very easy to fix, first remove the entire <rewrite> section from your web.config. Then just add new file (type global.asax) to your project if you do not already have it.
Add the following code in the Application_BeginRequest method:
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Raw URL
HttpContext context = HttpContext.Current;
string rawUrl = context.Request.RawUrl;
// Extra Path to be removed
const string EXTRA_PATH = "/MoreServices";
// Rewrite requests to /MyService to /MyService.svc
if (!rawUrl.Contains(".svc"))
{
// Index of last forward slash
int idxLastSlash = rawUrl.LastIndexOf('/');
// Insert .svc before last slash
rawUrl = rawUrl.Insert(idxLastSlash, ".svc");
}
// Remove extra path EXTRA_PATH from url
if (rawUrl.Contains(EXTRA_PATH))
{
// Remove extra path
rawUrl = rawUrl.Replace(EXTRA_PATH, "");
}
// Rewrite Path if it has been modified
if (rawUrl != context.Request.RawUrl)
{
// Note: Added "~" to avoid error: "The virtual path XX maps to another application, which is not allowed."
context.RewritePath("~" + rawUrl, false);
}
}
This will work the same way as the two rules above in the web.config. Hope this will help someone.
Related
i am trying to create a custom servervariable with url rewrite.
Url Rewrite for IIS generates the following config entry
<rewrite>
<rules>
<rule name="CName to URL - Rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.localfurnco\.de" />
</conditions>
<action type="Rewrite" url="?" appendQueryString="false" />
<serverVariables>
<set name="HTTP_MANUFACTURER" value="{C:1}" />
</serverVariables>
</rule>
</rules>
</rewrite>
But when iterating through the servervariables i can't find HTTP_MANUFACTURER.
The url rewrite seems to work but i can't get the Variable.
I am trying to call the address: test.localfurnco.de/subdir/webservice.asmx?wsdl.
C:1 should in this case be: "test".
I would be grateful for any suggestion and
thanks in advance
After trying some hours i figured it out myself.
Here is the resulting rule.
<rule name="CName to URL - Rewrite" stopProcessing="false">
<match url=".*" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{SERVER_NAME}" pattern="^(?!www)(.*)\.localfurnco\.de" />
</conditions>
<action type="Rewrite" url="?CustomValue={C:1}" logRewrittenUrl="true" />
<serverVariables>
<set name="HTTP_MANUFACTURER" value="{C:1}" />
</serverVariables>
</rule>
With this rule it is possible to access the ServerVariable "HTTP_MANUFACTURER" from C#:
string variable= httpContext.Current.Request.ServerVariables.Get("HTTP_MANUFACTURER");
A Different option a friend shared with me is this:
string value= HttpContext.Current.Request.Params.Get("CustomValue");
As you can see i rewrite the url and set a parameter which i assign the, by the pattern, filtered value.
I hope this helps somebody else.
I have the following code in my web-config to be able to redirect both the URLs with the prefix "www" and non-SSL requests to the https:// mydomain.com because the SSL certificate is registered to the domain without the www
<rewrite>
<rules>
<rule name="Remove WWW prefix and redirect to https" >
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" ignoreCase="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://mydomain.com/{R:1}" />
</rule>
</rules>
</rewrite>
This is the result:
1) http:// mydomain.com/something --> https:// mydomain.com/something (Correct)
2) http:// www.mydomain.com/something --> https:// mydomain.com/something (Correct)
3) https:// www.mydomain.com/something --> Shows certificate error (There is a problem with this website's security certificate.)
When you select "Continue to this website (not recommended)." on the certificate error page, the url is rewritten correctly (https:// mydomain.com/something)
How can I make sure the certificate error does not show?
Thank you
One way to solve it is to register two separate rules:
Remove www.
Force HTTPS.
<rule name="Remove www" stopProcessing="true">
<match url="(.*)" negate="false"></match>
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
so we use this in our projects, and this works.
Let me know it that helps:
<rewrite>
<rules>
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
</rewrite>
It also ignores the request when you access the site on your local machine.
I'm not sure about this as I don't have too much experience with url-rewriting but trying to help wont hurt.
You can try this
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://mydomain.com/{R:1}" redirectType="Permanent" />
I googled quite a lot and found this but it might not do what you meant to.
This problem cannot be solved by using rewrite rules: the problem is that the certificate is verified at the time the connection to the server is set up. As your server does not have a valid certificate for the www. variant, the certificate is invalid and the browser will notify its user.
Only after the user agrees to continue, the request is sent to the server and the rewrite rules kick in.
I'm seeing the same problem. Because the domain doesn't have an SSL certificate for www, the web.config code doesn't remove the www when the URL includes https. The result is using http with or without the www, correctly redirects it to https://domain, but if it starts with https: and www, it's stuck.
So can this be resolved at the DNS level so that www isn't defined as a CNAME and is just redirected there? Google Domains seems to have synthetic records for this. Has anyone used it successfully?
I'm trying to work in a rewrite problem, but its my first time and I guess I'm doing something very wrong.
A few users of our portal, try to navigate typing https://www.dudalina.proveagora.com instead the right one https://dudalina.proveagora.com. We have the SSL only for *.proveagora.com, so I started to try a redirect on the users who type the wrong url.
Here, the print of rewrite working great:
but, some users type https:// and some users type http://. By this way, I've tried to change the pattern.
I dont know why, but using the second pattern, the url https://www.dudalina.proveagora.com doesnt redirect anymore.
Just to explain a little more, the dudalina part is dynamic too. Dudalina is the store name, and it will change accordingly with store. So, we can have latter something like http://bestbuy.proveagora.com or anything else and this patterns much works with all the options.
I know, the user cant type www.bestbuy.proveagora.com but they do, and we need to make it works! :(
Any help?
EDIT
xml Web.Config
<rewrite>
<rules>
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(https\:\/\/|https\:\/\/)?([a-zA-Z]+\.)?(proveagora\.com){1}(\/.*)?$" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="http://dudalina.proveagora.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
Here you go:
<rule name="RemoveWWWPrefix" stopProcessing="true">
<match url="(.*)" negate="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTP_HOST}" pattern="^(.+\.)proveagora\.com$" />
<add input="{HTTP_HOST}" pattern="^dudalina\.proveagora\.com$" negate="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="https://dudalina.proveagora.com" appendQueryString="false" />
</rule>
What it will check is that the protocol used is HTTPS and that the subdomain of proveagora.com is present and different from dudalina.
If that's the case, it redirects to https://dudalina.proveagora.com
I have a website stored on azure for which I got an SSL certificate. I am trying to redirect www.mywebsite.com to https://www.mywebsite.com but with no success.
I am using the following code that should change the configuration of IIS where my project is deployed :
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{URL}" pattern="/$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
But when I type my URL it does not redirect to https.
By the way, rewrite appears unrecorgnized by Intellisense.
I can see that you've taken the code snipet from Steve Marx example
The problem is that you have whitespace in your rule name. Just remove them and it will work.
The name attribute of rule must not have spaces; the rule won’t work correctly in IIS on Azure with spaces in the name.
Find the full article here:
Azure https guide
I want to modify rewrite rule from C# code. Url Rewrite rule is resides in web.config file.
<system.webServer>
<rewrite>
<rules>
<rule name="partners">
<match url="^partners$" />
<action type="Rewrite"
url="partners.aspx" />
</rule>
<rule name="news">
<match url="^news$" />
<action type="Rewrite"
url="news.aspx" />
</rule>
<rule name="projects">
<match url="^projects$" />
<action type="Rewrite"
url="projects.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
I want to change for ex. <rule name="partners"> <match url="^partners$" /> to <rule name="partners"> <match url="^friendship/partners$" />,
how can I find node rule and update match url to "new one" where name = "partners";?
this is my idea for dynamic url rewriting. thanks for any other ways if you have.
I change value for connectionString in my web.config website with this code :
May be this example can help you (just change the value connectionString by system.webServer and add by rules etc..
Please tell me if it works for you
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load("../myPath/web.config");
foreach (XmlNode node in myXmlDocument["configuration"]["connectionStrings"])
{
if (node.Name == "add")
{
if (node.Attributes.GetNamedItem("name").Value == "SCI2ConnectionString")
{
node.Attributes.GetNamedItem("connectionString").Value = "new value";
}
}
}
step 1:- download urlrewrite2.exe Here
Step 2:- write you logic in web.config
<system.webServer>
<rewrite>
<providers>
<provider name="FileMap" type="FileMapProvider, Microsoft.Web.Iis.Rewrite.Providers, Version=7.1.761.0, Culture=neutral, PublicKeyToken=0545b0627da60a5f">
<settings>
<add key="FilePath" value="D:\j\branches\JuzBuzz\App_Data\rewriteurl.txt" />
<add key="IgnoreCase" value="1" />
<add key="Separator" value="," />
</settings>
</provider>
</providers>
<rules>
<rule name="FileMapProviderTest" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{FileMap:{R:1}}" pattern="(.+)" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
step 3:- Put you .txt file in App_Code folder or another place for which you have given the address in web.config , txt file will have data like
**technology,expert/search-expert.aspx?CatId=1
counselling-personal-growth,expert/search-expert.aspx?CatId=2** etc**
Microsoft has Microsoft.Web.Administration.dll available to help you out, but it requires administrator permissions to execute,
https://www.iis.net/learn/manage/scripting/how-to-use-microsoftwebadministration
It is quite suitable for a WinForms application (such as IIS Manager) to control an IIS server, but can also be used in other types of applications.
I do have a personal project that is a custom MWA implementation that works for some non-administrator cases. If you are interested in it, let me know.