JScottBranson JScottBranson - 3 months ago 22
Apache Configuration Question

301 Redirect to non-www ssl Version with Multisite/Shared Hosting

I have a shared hosting account with Godaddy.com that I use to host five different websites (site1.com, site2.com ... site5.com). I would like to place a .htaccess file in the / directory that causes all five different websites to redirect to the non-www and ssl versions of each of the sites.

Right now I have the following in my .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine On

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

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

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

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

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


The directory structure of the site is setup so that index.html for site1 is located at /public_html/index.html

The index files for the additional four sites are located in:
/public_html/[site#]/index.html
with four different directories for [site#] (site2/ site3/ site4/ site5/)

When I type the www version of the URL for sites 2-4 into my browser, the server automatically redirects to the https version of:

site#.com/other_sites/site#.com

I would like it if each of the sites redirects to ssl, even if visitors don't enter the www version of the site. I know I need an additional RewriteCond and RewriteRule, but don't know what code to use.

I am just learning RegEx and server configuration, so any help would be greatly appreciated.

Answer

You can use:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]