3

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.

5
  • Can we see the 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 the ServerAlias directive should only reference the alias, not the canonical ServerName as well. ie. ServerAlias www.example.com, not ServerAlias 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?
    – MrWhite
    Commented Aug 11, 2016 at 13:16
  • What kind of website are you running? Is it possible that the redirect is in the website code itself?
    – MrWhite
    Commented Aug 11, 2016 at 13:40
  • @w3dk Thanks for the hint. We had a wordpress installation with some plugins to do the redirect. I wish to upvote above comment, but not enough credits to do that. Thanks again!
    – Alavalathi
    Commented Aug 11, 2016 at 14:08
  • I've added an answer you can accept (removes question from unanswered queue). You'll need 15+ rep before you can upvote unfortunately. Just curious... you are running WordPress without "pretty" URLs (ie. no mod_rewrite in .htaccess or server config)?
    – MrWhite
    Commented Aug 11, 2016 at 14:13
  • 1
    I don't do the content development (done by another person) and was completely unaware of the wordpress settings etc. We do have the mod_rewrite enabled. And .htaccess does make some rewrites, but not performing redirects to www etc.
    – Alavalathi
    Commented Aug 11, 2016 at 14:32

1 Answer 1

1

From the redirect, it looks like this could be happening in the VirtualHost container for port 443.

Failing that, the redirect could be happening in the website code itself. (Which appears to be the case here - a WordPress plugin).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.