1

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

But I fail to put these two together.

Community
  • 1
  • 1
LuMa
  • 1,673
  • 3
  • 19
  • 41
  • Your force https rule seems to be a bit over kill on the conditions. Also you can simplify both rules also by using the domain name in the rule instead of variables. No need for variables if you only have the one site. – Panama Jack Dec 29 '15 at 20:30

1 Answers1

2

You can use:

RewriteEngine On

# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]

# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

If you test first for www and redirect to https, no need to test for http://www.

Or like @PanamaJack said, you just can use:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]

But you need to change the domain name.

Croises
  • 18,570
  • 4
  • 30
  • 47