iisreverse-proxyisapi-rewrite

Redirect HTTPS to HTTP for one directory while ISAPI rewrite in place


I've got a site (example.com) and a blog (externalblog.com) which is hosted externally and reverse-proxied using ISAPI rewrite. So if you go to www.example.com/blog you're actually on externalblog.com, but the URL is masked.

I've got this working with a number of sites, but the issue is when the parent site uses HTTPS, whereas the blog is only HTTP. When visitors attempt to follow old links, the blog posts will show normally under the https version of the url, but the stylesheets are all broken and a "no certificate" warning is shown.

I need an htaccess rule that will do 2 things:

  1. Forward anyone attempting to access an https version of a blog post only (ie anything within www.example.com/blog) to the http version instead.

  2. Forward anyone attempting to visit example.com/blog (without the www) to http://www.example.com/blog instead of the https version, which it currently does.

However I've like the rules to only affect www.example.com/blog and nothing else on example.com. My current rule for ISAPI rewrite on example.com is as follows:

RewriteRule ^blog(.+)$ http://www.externalblog.com$1 [R=301,L]

This works correctly and is based on an article online called "using reverse proxying to pull a wordpress blog into your domain" – I can't add any more links here due to not having enough reputation.

Any help would be much appreciated.

EDIT: I tried the following

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/blog [NC]
RewriteRule ^blog(.+)$ http://www.externalblog.com$1 [R=301,L]

This works as far as rewriting https to http – but reverse proxy URL masking no longer kicks in.


Solution

  • Your explanation is rather unclear, so sorry if my answer will be unrelated. I suppose you are trying to force everything under www.example.com/blog to be HTTPS, instead of HTTP? Then try this rule:

    RewriteEngine On
    
    # HTTP to HTTPS redirect rule
    RewriteCond %{HTTPS} off [NC]
    RewriteCond %{HTTP:Host} ^www\.example\.com$ [NC]
    RewriteRule ^(blog/.*)$ https://www.example.com/$1 [NC, R=301, L]
    
    # No www. to www. redirect rule
    RewriteCond %{HTTP:Host} ^example\.com$ [NC]
    RewriteRule ^(blog/.*)$ https://www.example.com/$1 [NC, R=301, L]
    
    # Your proxy rule goes here, I suppose it looks like:
    RewriteRule ^blog(/.*)$ http://www.externalblog.com$1 [NC, P]