2

So this is the scenario, I have already placed some 301 redirects via the web.config file due to a website redesign recently done. I also need to put in place the non-www to www url redirect and on top of that I will suggest to install an ssl certificate which I understand that needs a proper redirect as well.

How should I proceed with the redirects without messing things up or causing infinite loops?

1
  • 1
    web.config implies that you're running .NET, so is this served by IIS? Commented May 7, 2015 at 17:10

1 Answer 1

1

This htaccess snippet should do it for you -- redirects non-WWW to WWW mode, redirects non-HTTPS to HTTPS mode:

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ "https://www\.example\.com/$1" [R=301,L]

Sometimes for certain multi-app installs on a hosting account, you may work with "addon domains". These are basically masked subdomains of the host domain. In that case, you can add a couple more lines to really protect those utility routes from being used and forward them to HTTPS WWW as well:

RewriteCond %{HTTP_HOST} ^example\.hostdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.hostdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ "https://www\.example\.com/$1" [R=301,L]

Now where you put this depends on how specific your existing 301's are. If they use HTTP (or various WWW) explicit routes, then this snippet goes after them. If they are more flexible, or relative, and not based on HTTPS/WWW then the snippet can go before them, or wherever you like.

4
  • Thank you, I am going to put this to the test and let you know how it goes. Thanks again,
    – Jose Pena
    Commented May 6, 2015 at 15:24
  • @JosePena How did it go?
    – MrWhite
    Commented Nov 3, 2015 at 1:20
  • The above doesn't necessarily redirect "non-HTTPS to HTTPS". (http://www.example.com won't be redirected.)
    – MrWhite
    Commented Nov 3, 2015 at 1:23
  • 1
    @w3d at the end the https redirect wasn't needed since the client didn't gave the green light for the ssl. At the end I only used the web.config to list the redirects and that did the job needed.
    – Jose Pena
    Commented Nov 4, 2015 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.