Sitefinity CMS URLs and IIS URL Rewriting - iis-7

I have a site that is being served out of Sitefinity CMS, at http://www.example.com/, I also have sites on the same server that are being served out of the same root location that are accessed as follows: http://www.example.com/sub-site/. All of these sub-sites are in php and resolve to specific php files such as index.php. There are a lot of these sub-sites and we're looking to remove a number of them from the server. We usually send variables in the get request to these sub-sites as follows: http://www.example.com/sub-site/?address=12&ID=22
What we want to do is set up a rewrite rule that will redirect incoming requests to non-existent sub-sites to an error page written in PHP. This rewrite rule should keep the query string intact so that we can still use those variables once the error page is reached.
Here is the re-write rule that we have:
<rewrite>
<rules>
<rule name="RedirectFileNotFound" stopProcessing="true">
<match url=",*" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="errorpage.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
So this rewrite rule works terrific, in fact, it works too well. When I go to the Sitefinity site's home page all is well, but when I click on any of the links on that page, I'm redirected to the error page. I've determined that the problem has to do with dynamic server content as the Sitefinity site links look like this http://www.example.com/order/ or http://www.example.com/info/. These directories don't exist and thus, the rewrite rule catches them each time.
Is there a way that I can redirect the subsites without having the sitefinity site respond the same way?
I found this S.O. post that is addressing a similar problem, but I was unable to find much assistance there.

Related

Why is IIS stripping web page extensions all of a sudden?

This morning I pushed a new .Net web site (fw 4.6.1, previously 4.5.1) out to production. IIS (I assume) stripped the extensions from all web pages (ie. index.aspx just became index). I've had these web sites running for 8+ years using IIS rewrites. It looks like then IIS saw the "extensionless" web pages, the rewrite kicked in and sent it to a non-existent location, producing a 404.
I also did a Windows update 3 days ago (could have missed this issue since but unlikely). The rewrite rule basically says, if you can't find the page, look for the page in the "Clients" subdirectory. The rule looks like:
<rewrite>
<rules>
<rule name="Client Relocation" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Clients/{R:0}" />
</rule>
</rules>
</rewrite>
I was able to work around the issue by removing the rewrite rule in a subdirectory:
<rewrite>
<rules>
<remove name="Client Relocation" />
</rules>
</rewrite>
So, it would appear the extension was stripped, then IIS gets the page and thinks it doesn't exist so the rewrite rule takes effect.
So, my question is: who/what is stripping the extension and how can I stop it from doing so?
Lex Li put me on the right track with the comment about the application and failed request tracing. Turned out to be the App_Start/RouteConfig.cs file got modified and added:
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
Not exactly sure how or why but commenting out the AutoRedirectMode put the extensions back on. If I could give the answer to Lex's comment, I would have.

Restrict IIS virtual directory to specific HTTP host header?

I have a website setup in IIS with 2 HTTP Headers (www.mysite.com and [blank]).
There is a virtual directory called 'blog' which points to a WordPress site - this is accessed via www.mysite.com/blog/
Any other requests (e.g. www.site1.com, www.site2.com, something.mysite.com) are handled by the same site (using the [blank] header) and rewrite rules to display the necessary information.
The problem is I don't want /blog/ to be available to anything other than www.mysite.com, as presently I can access it with any of the following:
www.site1.com/blog/
something.mysite.com/blog/
This is because we are also using BlogEngine (for user blogs) and if they set their blog directory to be 'blog' then it clashes with the WordPress one.
Ideally I'd like to be able to add a host header to the virtual directory, but can't find a way to do that, so looking for any other solutions (other than moving the WordPress blog).
Thanks in advance
You can prevent browsing to /blog on non-www.mysite.com sites using a UrlRewrite rule:
<rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{URL}" pattern="/blog*" />
<add input="{HTTP_HOST}" pattern="www.mysite.com" negate="true" />
</conditions>
<action
type="CustomResponse" statusCode="403"
statusReason="Forbidden: Access is denied."
statusDescription="You do not have permission to view this directory or page." />
</rule>

Redirect arbitrary request to directory to particular page in ASP.Net

That title isn't very descriptive, but I couldn't figure out how to phrase my question very well. What I'm trying to do is use a single page to interpret multiple URLs. Here's an example: [domain]/name-of-question.aspx is clearly not a file on the site's server, and yet the server acts like it is. This behavior makes pages much more readable and more easily bookmark-able.
My vision for the solution is to be able to have to server redirect a request to a certain directory to a particular page, whilst appending the name of the page requested to the page as a URL parameter. Here's what I mean: [domain]/questions/name-of-question redirects to [domain]/question.aspx?page=name-of-question.
This is how reddit does their self posts, I think, but they don't use ASP.Net or IIS.
Is this possible, and if so, how would one implement this behavior? If there's any code you write, please write it in C#, because I don't know VB.Net very well. Thanks!
You need to use URL rewriting to accomplish this.
You have to create a rewrite rule that rewrites any requests to [domain]/questions/{1}
to [domain]/question.aspx?{1}
In ASP.NET you have the URL Rewriter module: http://www.iis.net/downloads/microsoft/url-rewrite
The rule might look similar to this and is applied in the web.config file:
<rewrite>
<rules>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="questions/(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="question.aspx?{R:1}" />
<serverVariables>
<set name="{RESPONSE_CONTENT_TYPE}" value="image/png" />
</serverVariables>
</rule>
</rules>
</rewrite>
EDIT: To change the content type, add the serverVariables section in the rewrite rule and authorise that variable to be set in IIS manager:
If you are using ASP.NET 4.0+, then this might be worth a read, as ASP.NET Routing and URL Rewriting are not necessarily competing technologies, but potentially complementary features.
URL Rewriting vs. ASP.NET Routing

IIS Rewrite not working (but redirection does)

I was trying to play with URL re-writing using the Rewrite Module 2.0 but I had no luck getting it to work. What I'm trying to do is re-write all calls to web app at port 80 to other applications hosted in IIS (or maybe on different servers on the network). Using the GUI provided by IIS I created the following rule:
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="site1/(.*)" />
<action type="Rewrite" url="http://localhost:7001/{R:1}" />
</rule>
</rules>
</rewrite>
Quiet simple, but unfortunately it does not work. On the other hand, when I change the action type to Redirect, it works fine.
What could be the problem?
I ran into this same issue yesterday, and it took me a long time to figure out.
The key here is that you've got an http:// prefix in your rewrite action; that makes this a special case that needs to be handled by Application Request Routing. The first step is to make sure that the Application Request Routing module is installed. You can find the module at https://www.iis.net/downloads/microsoft/application-request-routing. Once that is installed, go to your IIS web server (a level up from your web site), and open the Application Request Routing Cache feature. From the actions on the right, choose Server.Proxy.Settings, and make sure that the "Enable Proxy" checkbox is checked. This allows the URL rewrite task to be re-routed to Application Request Routing, and your reverse proxy should work for external requests.
The idea came from this excellent blog post from 2009: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
Stumbled across this old post when I was trying to solve the same issue.
SOLVED!
Using Rewrite URL feature in IIS Services Manager I created a friendly URL rule.
This worked ok and when I looked at the rule in the web.config file (www root) it showed 1 rule to redirect and 1 rule to rewrite.
I edited this to suit 1 match. Then I just duplicated this code editing the product ID for each. Example below:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^product\.php$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^id_product=\b35\b" />
</conditions>
<action type="Redirect" url="990mm-bohemia-cast-iron-electric-radiator"
appendQueryString="false" />
</rule>
The first rule looks for the string "product.php" in the URL and "id_product=35", it then redirects to "990mm-bohemia-cast-iron-electric-radiator" which currently does not exist. Then (see below)
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^\b990mm-bohemia-cast-iron-electric-radiator\b" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="product.php?id_product=35" />
</rule>
This rule rewrites the "product.php?id_product=35" bit to `990mm-bohemia-cast-iron-electric-radiator", creating the new location for the redirect.
Do make sure MVC routing doesn't steal your request. To prevent that from happening, ignore the route you're trying to rewrite:
RouteTable.Routes.Ignore("blog/{*pathInfo}");
Inspired by: https://sitecore.stackexchange.com/questions/3645/how-to-setup-a-reverse-proxy-with-sitecore
Change the Rewrite URL to AbsolutePath instead putting http://...
it should be
<action type="Rewrite" url="{R:1}" />
It worked for me, but in my case, I have been rewrite to a fixed webpage.

IIS Rewrite Wordpress User Authenticated

I want to be able to rewrite a url using url rewriting in IIS 7 whenever an anonymous user hits the home page of my wordpress site. Is there a way to identify whether the user is logged in or not as a condition of a rewrite rule?
Turns out this can be done by inspecting the cookie. In my case, I check to see whether HTTP_COOKIE contains the text 'wordpress'. If it does not, then I rewrite to a cached version of my page.
I also include a check to make sure we are on the www subdomain to avoid any conflict with another of my rewrite rules that is run when the url is on mydomain.com.
<rule name="HomePageCacheRewriteRule" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^$" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_COOKIE}" negate="true" pattern="wordpress" />
<add input="{HTTP_HOST}" pattern="www.mydomain.com" />
</conditions>
<action type="Rewrite" url="cached-home-page.htm" />
</rule>
The result is that anonymous users get the cached page, while logged in / recently logged out users see the standard page loaded by Wordpress.
Note: There are some additional checks that should possibly be added to handle some other scenarios. Here is a good article with a more robust example http://ruslany.net/2008/12/speed-up-wordpress-on-iis-70/

Resources