0

I have tried to piece together some suggestions found both here and elsewhere on using mod_rewrite to ensure www. as well as https:// on a site. I've come up with the following solution, but am curious if there is a more concise way to achieve the same goal.

<VirtualHost *:80>

  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^www.domain.com$
  RewriteRule ^(.*)$ https://www.domain.com%{REQUEST_URI} [R=301,L]

</VirtualHost>

<VirtualHost *:443>

  RewriteEngine On
  RewriteCond %{HTTPS} on
  RewriteCond %{HTTP_HOST} ^domain.com$
  RewriteRule ^(.*)$ https://www.domain.com%{REQUEST_URI} [R=301,L]

</VirtualHost>

Note that in my present use case I'm only trying to change the following cases:

  • http://domain.com -> https://www.domain.com
  • http://www.domain.com -> https://www.domain.com
  • https://domain.com -> https://www.domain.com

There are other subdomains that I do not want to redirect to https://, so I am only trying to match requests for the raw domain and the www subdomain. Any insight would be much appreciated.

4

1 回答 1

1

一体:

RewriteEngine On
RewriteCond %{HTTPS}_%{HTTP_HOST} ^(.*_domain\.com|off_www\.domain\.com)$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]
于 2012-10-29T22:51:16.617 回答