Solved

redirect non-www to www

Posted on 2014-10-06
9
170 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

23 Experts available now in Live!

Get 1:1 Help Now