Reputation: 477
I am trying to redirect all incoming requests in my site to: https://www.example.com
I am able to redirect HTTP traffic, with and without www, but I cannot get it to work with non-www HTTPS requests. I mean:
http://example.com
-> redirects correctlyhttp://www.example.com
-> redirects correctlyhttps://example.com
-> DOES NOT REDIRECTI have tried many rules in my .htaccess
file but none of them seem to work.
I am using Apache/2.2.15 on a CentOS machine. I also use mod_jk module to redirect all traffic to Tomcat.
My configuration files look as follow:
mod_jk.conf
LoadModule jk_module "/etc/httpd/modules/mod_jk.so"
JkWorkersFile /etc/httpd/conf/workers.properties
JkShmFile /var/run/httpd/mod_jk.shm
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
workers.properties
workers.apache_log=/var/log/httpd
worker.list=app1Worker
worker.app1Worker.type=ajp13
worker.app1Worker.host=localhost
worker.app1Worker.port=8009
app1.conf
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
CustomLog /var/log/httpd/app1_access.log combined
ErrorLog /var/log/httpd/app1_error.log
#<IfModule mod_jk.c>
JkMount /* app1Worker
#</IfModule>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
listening.conf
Listen 217.61.129.109:80
Listen 217.61.129.109:443
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,NE,R=301]
I have tried many other rules in the .htaccess
file but none of them have worked.
Could you tell the reason the last case is not working?
UPDATE-1
Sorry for the misunderstanding but I had only included parentheses because StackOverflow complained otherwise. Just ignore them. (Now removed)
The configuration of AllowOverride
is in the main httpd.conf
file of Apache:
DocumentRoot "/var/www/html"
<Directory />
Options SymLinksIfOwnerMatch
AllowOverride All
</Directory>
<Directory "/var/www/html">
Options Indexes SymLinksIfOwnerMatch
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I changed AllowOverride None
by AllowOverride All
because in the beginning this was the reason, .htaccess
file was not processed at all.
Now I have tested that this file is being processed because if I comment all its content then HTTP redirection doesn't work. My problem is only with non-www HTTPS redirection.
My feeling is that rules in .htaccess
are correct but there must be something else I am missing.
UPDATE-2 SOLVED!!!
Look below for the answer
Upvotes: 1
Views: 249
Reputation: 477
SOLVED!!!
Well I don't now why .htaccess filters didn't work for the special case mentioned but as per @MrWhite advice I get it to work with <VirtualHost>
configurations.
I updated my listening.conf file like this:
NameVirtualHost *:80
Listen 80
NameVirtualHost *:443
Listen 443
And my app1.conf like this:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
Redirect permanent / https://www.example.com/
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
CustomLog /var/log/httpd/app1_access.log combined
ErrorLog /var/log/httpd/app1_error.log
#Redirect all traffic to Tomcat
JkMount /* app1Worker
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
</VirtualHost>
Thanks for your help.
Upvotes: 2