Link to home
Create AccountLog in
Avatar of dotsandcoms
dotsandcoms

asked on

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

Hi,
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
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">
                     if(location.protocol=='http:')
                        {
                          var oldURL = window.location.hostname + window.location.pathname;
                          var newURL = "https://" + oldURL;
                          window.location = newURL;    
                        }  
    </script>
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

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

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

ASKER

Hi wboevink,

is it correct implementation?

            <rewrite>
                  <rules>
                      <rule name="HTTP to HTTPS redirect" stopProcessing="false">
                      <match url="(.*)" />
                      <conditions>
                          <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                      </conditions>
                      <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
                  </rule>
                  </rules>
            </rewrite>
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" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
                </rule>

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

here is the link :  airssential.com.au

it should automatically convert into https://www.airssential.com.au
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.
https://www.experts-exchange.com/questions/26243862/IIS-7-Redirection.html?sfQueryTermInfo=1+10+30+hades666+http+ii+redirect

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
http://www.iis.net/download/URLRewrite

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.

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

* 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.
http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/
 
Cheers,
Hades666    

Hi,
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;//yoursite.com work properly?  If so, then it an issue with iis urlrewrite and not ssl.

Do you actually have an ssl cert installed?

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

Check:

http://airssential.com.au

http://www.airssential.com.au
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.


http://airssential.com.au -> https://airssential.com.au
http://www.airssential.com.au -> https://www.airssential.com.au

Allthough the first one gives me an ssl certificate error.
Should http://airssential.com.au redirect to  https://www.airssential.com.au ? This will also get you rid of the error

https://airssential.com.au has an www.epichosting.com ssl certificate installed, which explains the error.
is it possible with  url rewrite to insert www ?
ASKER CERTIFIED SOLUTION
Avatar of Wouter Boevink
Wouter Boevink
Flag of Netherlands image

Link to home
membership
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