I know that this question has been asked and answered many times. However I can't seem to get it working for my scenario.
I want to redirect all http traffic to https and redirect the https root to a login page. Here are my virtual hosts in the vhost.conf file.
<VirtualHost *:80>
DocumentRoot /var/www
ServerName sub-domain.mydomain.com
ServerAdmin admin@example.com
CustomLog /var/log/httpd/http_access.log common
LOGFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
ErrorLog /var/log/httpd/http_error.log
TransferLog /var/log/httpd/http_transfer.log
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1
#Redirect permanent / https://%{HTTP_HOST}%/login/mylogin.jsp (Not used as I need to use a rewrite rule rather than redirect only the root)
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www
ServerName sub-domain.mydomain.com
ServerAdmin admin@example.com
CustomLog /var/log/httpd/https_access.log common
LOGFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
ErrorLog /var/log/httpd/https_error.log
TransferLog /var/log/httpd/https_transfer.log
SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /etc/httpd/conf/mydomain.crt
SSLCertificateKeyFile /etc/httpd/conf/mydomain.key
SSLCertificateChainFile /etc/httpd/conf/intermediate.crt
# HSTS (mod_headers is required) (15768000 seconds = 6 months)- ref https://mozilla.github.io/server-side-tls/ssl-config-generator/
Header always set Strict-Transport-Security "max-age=15768000"
JkMount .....
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
SSLRequireSSL
</Directory>
BrowserMatch "MSIE [2-5]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
AddDefaultCharset utf-8
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
RewriteEngine on
RewriteRule ^/$ /login/mylogin.jsp [R=permanent,L]
</VirtualHost>
The https root to login redirect works fine. However I am unable to get the http to https redirect working. I have tried various recommendations like;
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1
RewriteRule ^/?(.*) https://%{SERVER_NAME}/%$1
as per the various posts here. But Some of them don't work and on some of the recommendations there were comments mentioning that query strings don't work with the %{REQUEST_URI}
variable.
I need a good solution which will redirect all http traffic to https while retaining the rest of the request url. For example.
http://sub-domain.mydomain.com to https://sub-domain.mydomain.com
http://sub-domain.mydomain.com/somepage.html to https://sub-domain.mydomain.com/somepage.html
http://sub-domain.mydomain.com/thepage.html?day=tuesday&month=march to https://sub-domain.mydomain.com/thepage.html?day=tuesday&month=march
I will make the redirect permanent once I have tested and am satisfied with the redirect working as expected.
I don't want to use htaccess please.
You have already taken care of the specific redirection from "/" to "/login/mylogin.jsp", so it should work. For redirecting all the http traffic to HTTPS, you can use RedirectMatch.
<VirtualHost *:80>
DocumentRoot /var/www
ServerName sub-domain.mydomain.com
ServerAdmin admin@example.com
CustomLog /var/log/httpd/http_access.log common
LOGFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
ErrorLog /var/log/httpd/http_error.log
TransferLog /var/log/httpd/http_transfer.log
RedirectMatch permanent ^/(.*)$ https://sub-domain.mydomain.com/$1
</VirtualHost>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.