RewriteRule not working for apache https request

apache-2.2

We have requirement to convert all http requests to https requests and append .xyz.com to the server name. For that i have written the following rule which is working for http requests but does not work for https requests.

RewriteRule ^(.*)$ https://%{SERVER_NAME}\.xyz.com%{REQUEST_URI} [R]

If I type http:// lvldraspnetserv01, it redirects to https:// lvldraspnetserv01.xyz.com as required.

However, if I type https:// lvldraspnetserv01 it does not redirect to https:// lvldraspnetserv01.xyz.com, causing the certificate to fail.

Best Answer

Try it like this:

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} **Insert current condition here ***
RewriteRule ^(.*)$ https://%{SERVER_NAME}\.xyz.com%{REQUEST_URI} [R]

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} **Insert current condition here, but for https***
RewriteRule ^(.*)$ https://%{SERVER_NAME}\.xyz.com%{REQUEST_URI} [R]