Solved

redirect non-www to www

Posted on 2014-10-06
9
174 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 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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

791 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