Link to home
Create AccountLog in
Avatar of dotsandcoms

asked on

How to redirect url with www SSL from non-www and SSL ?

I need to redirect a web-site from a non-www to www url with SSL. I did www URL redirection, but not able to put SSL on redirected URL.

Site hosted on II7 with ASP.Net 4.0. Can someone please help to resolve the problem ?

www redirection code:
        string httphost = Request.ServerVariables["HTTP_HOST"].ToString();
        string pagename = Request.ServerVariables["SCRIPT_NAME"].ToString();
        string querystring = Request.ServerVariables["QUERY_STRING"].ToString();
        if (querystring != "")
            querystring = "?" + querystring;
        if (!httphost.Contains("www"))
            Response.Redirect("https://www." + httphost + pagename + querystring);
Avatar of satishratnaparkhi

Hi dotsandcoms,

You can use following code in your index page it will redirect to http to https

<script language="javascript" type="text/javascript">
                          var oldURL = window.location.hostname + window.location.pathname;
                          var newURL = "https://" + oldURL;
                          window.location = newURL;    
This is what I've been using for the http to https redirect.
Something similar can be accomplished for non-www to www, tell me if you need any help.

In the URL rewrite module inside the IIS7 management add an Inbound Rule

Match URL
-> Requested URL: matched the pattern
-> Using: regular Expressions
-> Pattern: (.*)
-> Ignore Case: checked

-> Input: {HTTPS}
-> Type: Matched the pattern
-> Pattern: OFF

-> Action type: Redirect
-> Redirect URL: https://{HTTP_HOST}/{R:1}
-> Append query string: checked
-> Redirect type: Found (302)
Avatar of dotsandcoms


Hi wboevink,

is it correct implementation?

                      <rule name="HTTP to HTTPS redirect" stopProcessing="false">
                      <match url="(.*)" />
                          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
It looks alright, this is my config:

                <rule name="HTTP to HTTPS" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{HTTPS}" pattern="OFF" />
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />

The more important question: is it working for you?
no its not working... Yours not working....

here is the link :

it should automatically convert into
Did you configure it using the management interface?
Do you see an URL Rewrite option in IIS7 management?
Avatar of Brad Howe
From a previous post I did.

You can use the new URL rewrite feature.  First make sure you have the latest version installed. Here is where to get the module:

Download the x86/x64 version for IIS 7.0

Once you have installed the module, you can simply copy and paste the following code between the <rules> and </rules> tags in your your web.config file in your website root directory. To simplify this for you, I have created the following rule for you.

                   <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                            <match url="(.*)" />
                                       <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                               <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />

* You can also see URLREWRITE in the IIS Administrator Console under the Features Tab.

If you want help creating import masks, please provide a list of all the sample URLS and there supposed redirects and we would be glad to help create an import_rules file for you.

Here is a great article on how to create 301 redirect rules.

I am able to do normal URL rewrtting something like product page.. but not able to do redirect on SSL URL. Is that require any settings on IIS ?
A redirect will work just fine as I posted above only if url rewirte module is installed. Now with that said, does https;// work properly?  If so, then it an issue with iis urlrewrite and not ssl.

Do you actually have an ssl cert installed?

yes, its already installed. https://domain gives me certificate error...while https://www.domain not gives any error.

Is the url rewrite module installed? It should work just fine with ssl.
yes url rewrite module is installed because i've test with a normal product page url rewrite
You're right. The redirect is working for me. -> ->

Allthough the first one gives me an ssl certificate error.
Should redirect to ? This will also get you rid of the error has an ssl certificate installed, which explains the error.
is it possible with  url rewrite to insert www ?
Avatar of Wouter Boevink
Wouter Boevink
Flag of Netherlands image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
thank you all for helping my problem...but solution given by wboevink worked perfectly. thank you once again