2

I am in the process of changing my protocol from http to https.

I have wordpress installed as a subdirectory (eg: www.example.com/blog)

I have the server behind a load balancer, requests to the load balancer are encrypted but requests from the load balancer to the server are not.

I updated the home and siteurl parameters to reflect the https adddress. And I added the following code to my wp-config.php file:

if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
       $_SERVER['HTTPS']='on';

For some reason, when I go to the url https://www.example.com/blog, the following redirects occur: https://...blog -> http://...blog/

http://...blog/ -> https://...blog/

I can live with one redirect (the one for adding the slash), but I don't understand why it redirects to the http:// address. I don't see anything in my setup that still references http://.

Why is it doing this?

I have tried clearing my browser's cache. I also have an .htaccess file but I suspect it doesn't have anything to do with it because the redirects occur even when all the code in the .htaccess file is commented

In someone still finds the htaccess file relevant: I have two rules in the htaccess file:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} =http

RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

for redirecting http to https

and

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

for adding the slash (I think).

Although the first rule isn't relevant (because I am going directly to the https address). And commenting the second rule has no effect on the issue in question.

Is there anywhere else in my setup where I need to update wordpress about the fact that I'm using https?

Thanks.

1
  • I use a similar configuration with LB responding to https but web server only http. I made two configurations: 1) set siteurl to https:// site.domain.com and 2) wp-config.php added $_SERVER['HTTPS'] = 'on';
    – user42826
    Commented May 9, 2017 at 20:23

4 Answers 4

1

This is somewhat of a guess, but it is probably a result of the web server configuration. What your server probably does is to see that the request want to load the blog url of the site. It checks out and sees that blog is a directory adds a slash and redirects to it. Now because the load balancer sends an http request the web server redirect to http. After that the request is made to the wordpress directory and all your htaccess and wordpress logic kicks in and provides the correct info/redirects etc.

Solution... if this is not only a good story, but also a true one, an http to https redirection in an htaccess of the root directory should probably fix it.

If it actually does fix the issue (and you think the fix worth your time) you will want to consider merging the "wordpress" htaccess into the root one as apache reads all the htaccess in each directory in the hierarchy on each request and "runs" them which is a pointless waste of time.

4
  • Re:The first paragraph: seems logical, this may be what is happening. Re:The second paragraph: I'm not sure I understand...I am using an Application load balancer. All patterns like /blog* reach my httpd server. I don't think there is a higher level directory where I can place an htaccess file.
    – theyuv
    Commented May 4, 2017 at 14:06
  • what is the definition of the apache virtual host? Commented May 4, 2017 at 14:51
  • Sorry, what do you mean? Are you asking what is the location of the httpd server?
    – theyuv
    Commented May 5, 2017 at 7:49
  • how do you know there is no higher level directory? Commented May 5, 2017 at 8:20
0

You need to update the baseurls in your database. Namely the option_values for siteurl and home in the options table.

The following code will do that for you robustly. Watch out for the prefix, which here I have indicated is "wp_" but which of course, it should not be! :) Also of course you need to change the site name:

UPDATE wp_options SET option_value = replace(option_value, 'http://www.example.com/blog', 'https://www.example.com/blog') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = REPLACE (guid, 'http://www.example.com/blog', 'https://www.example.com/blog');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://www.example.com/blog', 'https://www.example.com/blog');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://www.example.com/blog','https://www.example.com/blog');
8
  • Did you have any luck with this? Did my solution work for you? Commented May 2, 2017 at 7:46
  • Hey, I misunderstood your answer, I'm going to look into making these changes (siteurl and home have already been changed but the other 3 fields have not been) and get back to you. Thank you.
    – theyuv
    Commented May 4, 2017 at 8:49
  • siteurl and home are really the only two that could cause the problem though, so we may be on the wrong track. Let me know. Commented May 4, 2017 at 9:51
  • No this didn't work, perhaps it's something in my htaccess file. The redirect the redirect from https://..blog to http://...blog/ is the problematic one.
    – theyuv
    Commented May 4, 2017 at 10:27
  • ...but I thought the situation was the same with a commented .htaccess? Do you have server side caching in place? Nginx or Varnish for example. Commented May 4, 2017 at 10:45
0

Try this in your .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/

RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

See if it helps

1
  • This doesn't fix the issue
    – theyuv
    Commented May 4, 2017 at 14:00
0

You should not have to edit WordPress files yourself. WordPress can be used with either http or https without the need of manual code change.

If you have to change the protocol for an existing WordPress site, you can do two things:

  1. Make sure, your blog is accessible through http and https. Then switch to your WordPress settings in the backend and just update the blogs url from http://example.org to https://example.org WordPress should update the database and .htaccess-file itself

  2. you can do a search & replace on the database. To do so, you have to be aware of serialized data, which is not recognized with a simple search & replace process. I suggest you to use a tool like from interconnect

Make sure your wp-config.php does not contain a hardcoded url (see "Changing The Site URL"), which will overwrite your changes again.

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.