I make following post on redirection of https://www.website.com
to https://website.com
:
I can’t get to achieve this redirection and I don’t understand what is the reason.
If I type https://www.website.com
, it remains on https://www.website.com
and doesn’t perform the redirection to https://website.com
.
My config is a little special with a Zope server working with Apache2.
For the moment, here below are my rewrite rules (http://www.website.com
and http//website.com
are both redirected fine to https://website.com
) :
<VirtualHost *:443>
# REWRITE to get https://www.website.com to https://website.com except for cgi-bin scripts
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/cgi-bin/search [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/awstats [NC]
RewriteRule ^/(.*) https://localhost:8443/++vh++https:%{SERVER_NAME}:443/++/$1 [P,L]
</VirtualHost>
<VirtualHost *:80>
<IfModule mod_rewrite.c>
# www to non www for HTTP and HTTPS
RewriteCond %{REQUEST_URI} ^/www. [NC,OR]
RewriteCond %{REQUEST_URI} !^/podcast [NC]
# Rewrite below works : redirect 80 => https
RewriteRule ^/(.*) https://website.com/$1 [R=301,L]
RewriteRule ^/(.*) http://localhost:9674/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]
</IfModule>
</VirtualHost>
What could be wrong here?
2
Answers
I believe there are few things of note:
You also make use of L|last flags, which makes the engine stop processing further rules after
RewriteRule
is run.The above sorta gives you an idea how the engine runs your rewrites:
RewriteRules
are processed sequentially (unless you specifyL
flag, which you do for all rules)RewriteRule
can have manyRewriteCond
which all must match beforeRewriteRule
is considered. It also means thatRewriteCond
must be repeated for eachRewriteRule
individually (there’s however an interesting technique to group RewriteRules into if-then-else blocks)VirtualHost
context), only the URL-paths are matched by default unless you manually matchHTTP_HOST
variable.With this in mind, I see a few issues with your rewrite rules (I swapped http and https vhosts around for readability but it does not matter):
As far as I see – there’s no rule to rewrite
https://www.website.com -> https://website.com
, the only bit your https rewrite is checking is/cgi-bin
My suggestion would be along the following lines (it’s probably not a copy and paste solution, but hopefully you will have gotten the gist):
I had the same problem, for only one website of many that were virtually hosted on a amazon lightsail bitnami implementation.
After commenting out the line
In the /opt/bitnami/apache2/conf/httpd.conf file the problem was sovled.