Forum Moderators: phranque

Message Too Old, No Replies

non-www SSL Rewrite

         

ieapache

11:44 am on Apr 26, 2010 (gmt 0)

10+ Year Member



With Apache 2.2, I am unable to prevent a certificate error when rewriting non-www SSL requests. I can get the rewrite to work, but the cert warning pops up BEFORE the redirection takes place. I've been moving variations of the code below in different locations in the conf files but can't seem to find the proper context.

httpd.conf
extra/httpd-vhosts.conf
extra/httpd-ssl.conf

<Directory "/var/www/html/domain">
RewriteEngine on
# redirect non-cert domains to non-secure original domain
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

#redirect non-www domain requests
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.*)$ http://www\.example\.com/$1 [R=301,L]
</Directory>

[edited by: jdMorgan at 1:24 pm (utc) on Apr 26, 2010]
[edit reason] example.com [/edit]

jdMorgan

1:23 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need to have a "wild-card" certificate that applies to both www- and non-www for this to work.

As you observed, the SSL auth checks are done as the initial connection is being established, before any server-side config code might change anything about the request.

Also, be aware that the code you posted has been incorrectly modified. The periods in the substitution URL of the second RewriteRule should not be escaped. Also, it will always redirect to http as written above.

If you wish to redirect non-www to www while maintaining the original http/https protocol, then it should read:

RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]


Additionally, the first RewriteCond in the first rule appears to be incorrect. If the comments describe the desired function, then it should read:

RewriteCond %{SERVER_PORT} =443

This is server config code. Be very sure that you understand every single character, the function of each line and each rule, and what their effects will be -- both on your server and on search results.

Jim

[edited by: jdMorgan at 1:36 pm (utc) on Apr 26, 2010]

ieapache

1:28 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



Thanks!

jdMorgan

1:37 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We cross-posted while I was expanding the reply above... Please review additional info.

Jim