Solved

redirect non-www to www

Posted on 2014-10-06
9
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 7

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 7

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
 
LVL 7

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 7

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 7

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

#SSL #TLS #Citrix #HTTPS #PKI #Compliance #Certificate #Encryption #StoreFront #Web Interface #Citrix XenApp
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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