Link to home
Start Free TrialLog in
Avatar of FocIS
FocIS

asked on

Simple http to https redirect on iis7

Not being a web person, i have to ask this seemingly easy question.  Random guides found on a google search are complicated because we already have a url-rewrite in place

we want to make it so anyone who goes to our site at http://www.example.com by http, are on-the-fly and blindly redirected to https://www.example.com while maintaining our existing url-rewrite

Our existing rewrites are handled by web.config it the root of the site and is in the code block below.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Rewrite rule1 for CRMsoftwareTest" enabled="true" stopProcessing="true">
                    <match url="(CRMsoftware|api)(.*)" negate="true" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="CRMsoftware/portals{REQUEST_URI}" appendQueryString="false" redirectType="Found" />
                </rule>
            </rules>
            <rewriteMaps>
                <rewriteMap name="CRMsoftwareTest" />
            </rewriteMaps>
        </rewrite>
    </system.webServer>
</configuration>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FocIS
FocIS

ASKER

beautiful.  thanks for this
Avatar of FocIS

ASKER

Hello, one follow up question regarding this if you can please help still - still easy...

all of our email signature blocks contain JPG images that were affected by this rewrite

while http://www.example.com/whatever.jpg and
while https://www.example.com/whatever.jpg
ARE both valid and work... the signature images are hard coded to http and must be getting lost in translation, they apppear now as boxes with a red X indicating the file was moved or not availalbe (i guess outlook can't deal with redirects in images)

what code could i put in, or change, to exclude http requests for anything ending in .jpg, or i could also list specific url's of each of the 4-5 images if that helps

sorry for following up after accepting/closing, hopefully it's ok
No worries. Just try adding an additional condition
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
    <conditions>
      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      <add input="{REQUEST_URI}" negate="true" pattern="^*.jpg$" ignoreCase="true" />
    </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

Open in new window

I haven't tested this, but it will be something along those lines.
Avatar of FocIS

ASKER

i think i'm close but every time i make it live, the main site is http error 500, and looking at the rules in the iis gui says
line number 16, error, config file is not well-formed xml

here's what i have in there (line 16 is the closing of </conditions>

i tried to specify the certain logo .jpg files so it doesn't interfere with actual jpg's embedded in the main site


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  <match url="(.*)" />
    <conditions>
      	<add input="{HTTPS}" pattern="off" ignoreCase="true" />
	<add input="{REQUEST_URI}" negate="true" pattern="^.*logo1.jpg$" ignoreCase="true">
	<add input="{REQUEST_URI}" negate="true" pattern="^.*logo2.jpg$" ignoreCase="true">
	<add input="{REQUEST_URI}" negate="true" pattern="^.*logo3.jpg$" ignoreCase="true">
	<add input="{REQUEST_URI}" negate="true" pattern="^.*logo4.jpg$" ignoreCase="true">
	<add input="{REQUEST_URI}" negate="true" pattern="^.*logo5.jpg$" ignoreCase="true">
    </conditions>
  <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>

Open in new window

<add input="{REQUEST_URI}" negate="true" pattern="^.*logo1.jpg$" ignoreCase="true">

You are missing closing tags. the end of each of the new lines should be /> and not just >
Avatar of FocIS

ASKER

Learning as we go here, thanks - i'm greatful for your continued help

Here's the exact code i have which results in the 500 still, but removing my logo lines leaving the https still works fine of course

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
		<rule name="HTTP to HTTPS redirect" stopProcessing="true">
  			<match url="(.*)" />
    			<conditions>
      			<add input="{HTTPS}" pattern="off" ignoreCase="true" />
			<add input="{REQUEST_URI}" negate="true" pattern="^.*logo1.jpg$" ignoreCase="true" />
			<add input="{REQUEST_URI}" negate="true" pattern="^.*logo2.jpg$" ignoreCase="true" />
			<add input="{REQUEST_URI}" negate="true" pattern="^.*logo3.jpg$" ignoreCase="true" />
			<add input="{REQUEST_URI}" negate="true" pattern="^.*logo4.jpg$" ignoreCase="true /">
			<add input="{REQUEST_URI}" negate="true" pattern="^.*logo5.jpg$" ignoreCase="true" />
    			</conditions>
  			<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
		</rule>
		<rule name="Rewrite rule1 for CRMsoftware" enabled="true" stopProcessing="true">
                    <match url="(CRMsoftware|api)(.*)" negate="true" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="CRMsoftware/portals{REQUEST_URI}" appendQueryString="false" redirectType="Found" />
                </rule>
            </rules>
            <rewriteMaps>
                <rewriteMap name="CRMsoftwaretest" />
            </rewriteMaps>
        </rewrite>
    </system.webServer>
</configuration>

Open in new window

Avatar of FocIS

ASKER

oh, there it is i see the " outside the slash :)
you can replace all those with a

<add input="{REQUEST_URI}" negate="true" pattern="^.*logo[0-9]+.jpg$" ignoreCase="true" />