Solved

redirect non-www to www

Posted on 2014-10-06
9
172 Views
Last Modified: 2014-10-08
Normally this would not be an issue with a simple modrewrite but this one is a little different.

1. I have 2 domains that share the same content.

What I want to do is redirect

domain1.com to https://www.domain1.com

OR

domain2.com to https://www.domain2.com

I am not sure how to put in a IF ELSE with .htaccess

Also, if they enter www.domain1.com or www.domain2.com and it's NOT SSL I want to redirect to SSL.
0
Comment
Question by:Robert Saylor
  • 5
  • 4
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40364833
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\\. [NC]

RewriteRule .? https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Open in new window

0
 
LVL 6

Author Comment

by:Robert Saylor
ID: 40365701
The rule is adding www.www.domain not sure why. I've been messing with your code.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40366042
RewriteEngine on
RewriteCond %{HTTPS} off 
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Open in new window

0
 
LVL 6

Author Comment

by:Robert Saylor
ID: 40366247
If I put in http://domain1.com/reservations/ it outputs https://domain1.com/reservations//

My SSL certificate is on www.domain1.com and www.domain2.com

The reservations directory is a symbolic link that is shared between the two domains so the domain does not change when going to a shared program.

This might be a loosing battle and found the issue when an internal user linked to the page wrong. But I will be linking the URL from our main page correct so the chances of coming to the SSL site incorrect will be small.

I thought about just adding to the SSL file the non www names and that would correct the SSL warning issue on the 301 redirect.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 6

Author Comment

by:Robert Saylor
ID: 40366264
FYI Here is the site:

www.aggressor.com/reservations/
www.dancerfleet.com/reservations

I have PHP doing the redirect for non SSL but when they enter this way PHP can't fix it and thats why I was looking at .htaccess

https://aggressor.com/reservations/

Gives SSL error but I wanted to redirect https://aggressor.com/reservations/ to https://www.aggressor.com/reservations/

Same for dancerfleet.com
0
 
LVL 58

Expert Comment

by:Gary
ID: 40366392
Realised my mistake earlier

RewriteEngine on

RewriteCond %{HTTPS} off 
RewriteRule ^(.*) https://%{HTTP_HOST}/$1

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301]

Open in new window

0
 
LVL 6

Author Comment

by:Robert Saylor
ID: 40366616
I got the code in but it's still doing the same.

https://aggressor.com/reservations/ does not redirect to www
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40366689
RewriteEngine On  

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

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301]

Open in new window

0
 
LVL 6

Author Comment

by:Robert Saylor
ID: 40368925
Gary,

I am going to have to cancel this one. I was not able to get it to work and because it's going to be linked from our site the chances of the user entering the domain incorrectly will be very slim. What I will most likely do is purchase 2 more SSL domains for the non www. We have a multi domain certificate so I only need to add 2 more to the cert. I will do that if I see issues with users accessing.

Thanks!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with .htaccess file 4 66
ports for sccm 2012 1 73
Windows - create strong certificates 5 51
how to use external config file with Spring MVC 4 88
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now