IIS Rewrite Wordpress User Authenticated - wordpress

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/

Related

IIS URL Rewrite Domain change 301 redirect individually only some pages and all other pages to another URL

I am new to IIS URL Rewrite rule Regex and looking for help here.
This is a sitecore web site which is built in ASP.NET MVC. I have a one domain i.e. www.xyz.com get merged with the another domain i.e. www.abc.com . I have the 1 to 1 mapping of both www.xyz.com/test -> www.abc.com/test1 . ALl the internal pages of www.xyz.com/xx will be mapped with the www.abc.com/foo except Homepage of www.xyz.com
I am currently stuck at how can i,
1) Redirect www.xyz.com/ homepage to a third party URL?
2) At the same time keep the 1-1 mapping of www.xyz.com/internal pages with www.abc.com/internal pages
I am using IIS URL Rewrite rule to do a domain redirect of www.xyz.com to www.abc.com and looking for additional help with the homepage redirect with a third party page
Thanks in Advance!
you just need two different redirect rules, the first rule will check for the root domain and redirect to third party url and the second rule will check for internal pages and redirect to internal pages on the new domain
it will look something like this
<rule name="redirect root to third party" stopProcessing="true">
<match url="^$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="(www.)?xyz\.com$" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="<third party url here>" />
</rule>
<rule name="redirect to new domain" stopProcessing="true">
<match url="/(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?xyz\.com$" />
</conditions>
<action type="Redirect" url="http://<new domain>/{R:1}" redirectType="Permanent" />
</rule>
P.S: in the second rule, please check for / -- i have not tested the rule myself there could be an extra slash introduced in the destination url or a slash removed, whatever it is you will be able to fix it. The main point here is you need two separate rules one for checking the exact domain with nothing after it and redirecting to third party and another rule for sub page redirects

Using web.config to configure multiple websites/domains

I have several shared hosting accounts with Newtek. They told me one account can be used to host several websites, but I don't know how to do that.
They said it can be done using either an .htaccess or web.config file. And since it's ASP.NET, that suggests web.config is the way to go.
Does anyone know how to use web.config to route requests for a given domain to a subfolder? I assume that subfolder would need to be set as an application starting point, which it appears their control center will allow me to do. I just need to associate that starting point with a particular domain.
And does this sound like a reasonable approach (shy of forking out money for a virtual server)?
If the IIS URL Rewrite Module is installed, you can create a Rule to redirect the request to a subfolder based on the domain name.
<rule name="site2.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?site2.com" />
<add input="{PATH_INFO}" pattern="^/site2/" negate="true" />
</conditions>
<action type="Rewrite" url="\site2\{R:0}" />
</rule>
Source: https://weblogs.asp.net/owscott/iis-url-rewrite-hosting-multiple-domains-under-one-site

IIS URL Rewrite for Clean URLs

I am working on a site that uses IIS. I have it working to remove extensions but then I was informed that the client was everything removed after the trailing slash.
I first thought this might not be possible with URL Rewrite, but then again I don't know much about IIS.
Basically if anyone browses the site and clicks on an interior page the url should stay the same, the top level domain.
http://www.example.com instead of http://www.example.com/whatever
this is what I have so far to remove the extension.
<rule name="Remove PHP Extension">
<match url="(.*)\.php" />
<action type="Rewrite" url="{R:1}" />
</rule>
<rule name="Remove PHP Extension">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.php" />
</rule>
Any help would be greatly appreciated
Thanks
Doing it via IIS may be difficult since each link would actually have to navigate the user to the page in the first instance. And then you're stuck, unless you then redirect to the homepage and then perform a rewrite based upon the referrer - which is all a bit nuts.
Instead, you could use an javascript/jquery approach which may be better. Something like:
$('a').on('click', function(ev){
ev.preventDefault();
$('body').load($(this).attr('href'));
})
This would take any anchor and override it ensuring th the content was loaded into the current page.
Admittedly, this is without any conditional checks for the href pointing to an page id etc. and also falls short when considering you may already have ajax calls in tht esite.
It also doesn't take into consideration forms submittals, but you could work this in.
Another problem with this is what would happen if someone navigated to a page directly.
A final approach would be to load the entire site into an Iframe. Nasty but apprpiate if that is the effect the client wants. You could have some javascript on each page that forced itself into an iframe if it wasn't already.
Whichever approach you take, your client can kiss goodbye to any search rankings. Which reminds me, don't forget to disallow all in your robots.txt

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

Sitefinity CMS URLs and IIS URL Rewriting

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.

Resources