Solved

redirect non-www to www

Posted on 2014-10-06
9
176 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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