Kentico app in sub folder and IIS URLRewrite? - iis

I am hosting a site on winhost and I am using IIS URLRewrite to allow for sub folders on the host to be mapped to sub domains.
i.e.
~/
~/myapp/
~/KenticoCMS/
with IIS URL Rewrite rules in the root web.config to route requests for 'mydomain.com' to route to ~/KenticoCMS/ and requests for "myapp.mydomain.com' to route to ~/myapp/
Currently when I disable the rewrite, mydomain.com/KenticoCMS/ comes up fine.
However when I enable to rewrite, I get an exception:
[ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex]
System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +10698899
CMS.URLRewritingEngine.URLRewriter.CheckPermissions(String siteName, PageInfo pi, Boolean excludeSystem) +235
CMSAppBase.CheckSecurity() +775
CMSAppBase.CMSAcquireRequestState(Object sender, EventArgs e) +606
CMS.CMSHelper.CMSApplicationModule.app_AcquireRequestState(Object sender, EventArgs e) +22
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
Anyone have any suggestions on how to configure the site so that it can work in this setup?
Edited to add web.config from root folder with re-write code:
I believe, however, the issue is with the Kentico app thinking it is in a sub folder (which it is) but not getting that sub folder via the URL.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<rewrite>
<rules>
<rule name="Rewrite to Kentico" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Rewrite" url="KenticoCMS/{R:1}" />
</rule>
<rule name="Rewrite to Myapp" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^myapp.mydomain.com$" />
</conditions>
<action type="Rewrite" url="myapp/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

so this is a pretty old question, but the short answer is that I think your assumption is correct, Kentico does not like that the URL has been altered in this way.
I have done something similar before which should be helpful if anyone else has this issue, but it does a redirect instead of a rewrite.
I setup the following rule:
<rule name="all subdomains" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="(myapp)\.example\.com" />
</conditions>
<action type="Redirect" url="http://example.com/{C:1}{URL}" redirectType="Found" />
</rule>
Obviously, you can change the redirect type etc. but the key thing is it's a redirect and not a rewrite, so it's not entirely what you're pointing at in the original question.
Assuming however that you have/had valid licence keys for the subdomains, I'm not sure why you would need or want to do this with a rewrite, as you can just setup an IIS site per subdomain.

Related

Windows server2012r2 how to create multiple subdomain site

I'm creating an application that accepting users as sub-domains,
Ex: {user_id}.mywebsite.com, thus, every request has to be *.mywebsite.com.
Problem is that every sub domain has to be bind on the iis
user1.mywebsite.com
user2.mywebsite.com etc etc
My question is, is there a way to set a domain site to accept every sub-domain request?
This is basically what I want to do
Is there any way to do it without adding every user as sub-domain to the iis site?
Thanks in advance.
You can set up a catch-all site and then use ARR to forward the traffic,
<system.webServer>
<rewrite>
<rules>
<rule name="site1" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*).site1.com$" />
</conditions>
<action type="Rewrite" url="http://localhost:8091/{R:0}" />
</rule>
<rule name="site2" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*).site2.com$" />
</conditions>
<action type="Rewrite" url="http://localhost:8092/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Other supporting steps can be found in this blog post,

IIS rewrite rule is not working in Azure production

I have website with multiple domains and i want them to have different robots.txt
I have rule for this so depends on domain request it will return different robots. This is workin beta slot but after deploy to production slot its not working.
<rule name="robots" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="^robots.txt" />
</conditions>
<action type="Rewrite" url="https://{HTTP_HOST}/{HTTP_HOST}.robots.txt" />
</rule>
If i am changing action type to redirect it seems to work because webbrowser redirects and is in stuck with to many redirects (Which make sense.)
As url-rewrite-module-configuration-reference mentioned that the server variable REQUEST_URI can be used to access the entire requested URL path, including the query string. Assuming that the URL for robots.txt is https://<your-hostname-and-port>/robots.txt, then you would get the string /robots.txt as an input via REQUEST_URI.
<rewrite>
<rules>
<rule name="robots" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_URI}" pattern="^/robots.txt" />
</conditions>
<action type="Rewrite" url="{HTTP_HOST}.robots.txt"/>
</rule>
</rules>
</rewrite>
With the rule above, I could rewrite my URL and get the following result:
Changing the type of the rule action from Rewrite to Redirect, I could get the following result:

IIS redirect to full domain name

I need to redirect from
http://someserver/someapplication.page.aspx
to
http://someserver.domain.com/someapplication.page.aspx
Both the requests lead to the same server.
someserver/ works through our company's internal DNS
This is the same question as Redirecting to Full Domain
but I want an IIS solution for this, not code. My guess is it will have something to do with adding a httpRedirect add element in Configuration Editor using wildcards.
You can use URL Rewrite for that which is the recommended way to do it in IIS, simply add a web.config with a rule like:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to full domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^someserver$" />
</conditions>
<action type="Redirect" url="http://someserver.domain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Remove multiple forward slashes

I've noticed that with .NET MVC sites, you're able to hit URLs with multiple forward slashes, for example:
http://www.example.com//category
http://www.example.com//category//product
The URL loads fine and everything works, however, I've been asked to stop this from happening.
I've been trying to use IIS URL Rewrites to get it working:
<rewrite>
<rules>
<rule name="Remove multiple slashes" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{UNENCODED_URL}" matchType="Pattern" pattern="^(.*)//(.*)$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{C:1}/{C:2}" />
</rule>
</rules>
</rewrite>
However, the results seem very temperamental. Sometimes the product URL will redirect, sometimes it won't and the same thing happens with the category. It's almost like the URL is being cached by the application.
Does anyone know if I can disable any caching that's in place, or if there is another way to get around this multiple slash issue?
Any help is much appreciated.
In the end, I have resorted to using a code behind redirect to get it working.
The issues I was having using the IIS URL Rewrites was due to the way IIS caches the redirects. When I disabled caching completely, as WouterH suggested, it worked. However, I'm not comfortable disabling caching in this way as it could introduce performance issues.
My fix was to use a code behind redirect in the Global.asax.cs file:
protected void Application_BeginRequest(object sender, EventArgs e)
{
string requestUrl = Request.ServerVariables["REQUEST_URI"];
string rewriteUrl = Request.ServerVariables["UNENCODED_URL"];
if (rewriteUrl.Contains("//") && !requestUrl.Contains("//"))
Response.RedirectPermanent(requestUrl);
}
I would have liked to use IIS URL Rewrite to get this working, unfortunately I didn't have the time to continue down that line.
Interestingly, the below method did work, however, the HTTP_X_REWRITE_URL is added by the Helicon ISAPI Rewrite which I'm running locally, but is not available on our production server.
<rewrite>
<rules>
<rule name="Remove multiple slashes" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{REQUEST_URI}" />
<conditions>
<add input="{HTTP_X_REWRITE_URL}" pattern="([^/]*)/{2,}([^/]*)" />
</conditions>
</rule>
</rules>
</rewrite>
URL Rewrite Module
As IIS automatically normalizes url's with double slashes, you can try to redirect to the normalized url like this:
<rewrite>
<rules>
<rule name="Remove multiple slashes" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{REQUEST_URI}" />
<conditions>
<add input="{UNENCODED_URL}" pattern="(.*?)[/]{2,}$" />
</conditions>
</rule>
</rules>
</rewrite>
You can also try to disable caching of the URL rewrite module:
Disabling internal caching of rewrite rules
To disable caching of inbound rewrite rules in URL Rewrite Module run
the following command from an elevated command prompt:
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Rewrite /v
RewriteCacheEnabled /t REG_DWORD /d 0
I have a small feeling that you'll have to restart the webserver after this change :)
Other option #1: non-cacheable server variable
This idea just popped into my mind:
use a non-cacheable server variable in the rule, f.e. try with HTTP_USER_AGENT
<rewrite>
<rules>
<rule name="Remove multiple slashes" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{REQUEST_URI}" />
<conditions>
<add input="{UNENCODED_URL}" pattern="(.*?)[/]{2,}$" />
<add input="{HTTP_USER_AGENT}" pattern=".*" />
</conditions>
</rule>
</rules>
</rewrite>
You can explore other server variables here
Other option #2: clear browser cache
During web development, make sure you use Ctrl+F5 to refresh your page, or clear your browser cache after making changes like updating rewrite rules etc. Otherwise you can spend hours of watching to the same problem while it was just your browser that needed to refresh its cache.
Other option #3: IIRF
If you really can't get it to work with the IIS URL Rewrite Module, you can give the open-source ISAPI module IIRF a try. It accepts rules similar to mod_rewrite for Apache.
Try following
<rule name="RemoveMultipleSlashes" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="{REQUEST_URI}" />
<conditions>
<add input="{UNENCODED_URL}" pattern="([^/]*)/{2,}([^/]*)" />
</conditions>
</rule>

IIS7.5 URL Rewrite rule to perform a 301 redirect from mysite.hosting.com to mysite.co.uk

I use IIS 7.5
I have a website wich has a valid host like:
A) mysite.co.uk
and a DEFAULT host (using for testing proposes provided by the hosting company):
B) mysite.hosting.com
Website is visible on both address, creating a DUPLICATE CONTENT issue for Search Engine.
I need redirect all the traffic (for all pages) from B to A using a 301 redirect.
IIS7.5 Http Redirect it is not design for this situation so I suppose to use IIS 7.5 Url Rewrite Module.
My questions: how write the ROLE in my web.config? Thanks
Try adding something like this between the <System.webServer> tags in your web.config:
<rewrite>
<rules>
<rule name="Redirect mysite.hosting.com to mysite.co.uk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="mysite.hosting.com" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</rules>
</rewrite>
Alternatively, you can do this using global rules by adding:
<rewrite>
<globalRules>
<rule name="Redirects to mysite.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="mysite.hosting.com$" />
</conditions>
<action type="Redirect" url="http://mysite.co.uk/{R:0}" />
</rule>
</globalRules>
</rewrite>

Resources