So I have a webserver running on Apache, with a few virtual hosts. I want to default to https
and hence I have a rewrite rule.
<VirtualHost *:80>
ServerName example.com
ServerAlias example.com www.example.com
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
The interesting thing is that, I always see multiple 301 happening. ie
http://example.com/something
-> https://example.com/something
-> https://www.example.com/something
I thought it might be the ServerAlias
that is causing the issue, so I split the conf to 2 separate rules
<VirtualHost *:80>
ServerName example.com
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>
Still getting the same result. I don't have any rules anywhere to redirect to www
, user enters example.com and without www
, and I'm confused where does the second redirect come from.
I also don't have any .htaccess
rules to perform redirects. What am I doing wrong here ?
Edit : Problem identified as a wordpress plugin doing the redirect. Thanks for hints from community.
VirtualHost
for port 443 - since that is where the redirect appears to be happening? You appear to only need one VirtualHost container for port 80, but theServerAlias
directive should only reference the alias, not the canonical ServerName as well. ie.ServerAlias www.example.com
, notServerAlias example.com www.example.com
- although that's not the cause of your problem. Note that the directives above are all 302, yet you say you see multiple 301?