If I access directly to https it works, but the site is still accessible by HTTP. How do I force all http to https?

I have used WordPress HTTPS plugin. But it is not redirect to https.

I have configured the nginx with following:

server {

    listen       xxx.x.xxx.xxx:80;

    server_name mydomain.com;

    root   /var/www/html/mydomian.com/;
    index  index.html index.htm index.php;
    charset utf-8;

    location / {
            try_files $uri $uri/ /index.php?$args;
    rewrite ^(.*)$ https://$http_host$request_uri redirect;

But this redirection will leads to infinite loop. I installed the
CloudFlare Flexible SSL, which said to solve the infinite loop, but still not helping.

Any other way to force the http redirect to https?


My ssl block:

   server {

       listen       xxx.x.xxx.xxx:443 ssl;
       listen       xxx.x.x.x:443 ssl;

    server_name  $hostname xxx.x.xxx.xxx;

    ssl                    on;
    ssl_certificate         /etc/nginx/ssl.crt/server.crt.combined;
    ssl_certificate_key     /etc/nginx/ssl.key/server.key;

    root   /var/www/html;
    index  index.html index.htm index.php;

    location ~^/~(?<userdir_user>.+?)(?<userdir_uri>/.*)?$ {
            alias /home/$userdir_user/private_html$userdir_uri;
            index index.html index.htm index.php;
            autoindex on;

            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include /etc/nginx/fastcgi_params;
                fastcgi_index index.php;

                #try_files does not work after alias directive
                if (!-f $request_filename) {
                    return 404;

                fastcgi_param DOCUMENT_ROOT /home/$userdir_user/private_html;
                fastcgi_param SCRIPT_FILENAME $request_filename;



    location ~ \.php$ {
            try_files $uri =404;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_pass unix:/usr/local/php55/sockets/webapps.sock;


    include /etc/nginx/nginx-info.conf;
    include /etc/nginx/webapps.ssl.conf;
  • This may help stackoverflow.com/questions/35143193/… – prosti Dec 31 '16 at 17:27
  • @prosti, nope, still getting in infinite redirection loop. – dev-jim Dec 31 '16 at 18:19
  • The settings on cloud flare is what I would try to alter as the first thing. BTW, I used let's encrypt (free) https. You may consider that @dev-jim – prosti Jan 3 '17 at 21:38

There are several ways to enable https on your wordpress site.

You need to update your site urls including https in it if you have access to your dashboard

Wordpress settings page screenshot

You can also define both the WP_SITEURL and WP_HOME in your test wp-config.php

define( 'WP_SITEURL', 'http://example.com.mytestdomain.com' );
define( 'WP_HOME', 'http://example.com.mytestdomain.com' );

In relation to your nginx config:

You can use an if block instead of nesting the rewrite in your base location

if ($host ~* ^example\.com$) {
    rewrite ^(.*)$ https://example.com$1 permanent;
| improve this answer | |
  • I did change WP_SITEURL and WP_HOME to https. And also add the if block that you suggested. Ever since I added the if block, I got this message in FF Firefox has detected that the server is redirecting the request for this address in a way that will never complete., which means infinite redirect loop again. – dev-jim Dec 31 '16 at 17:12
  • Do you have a separate location block listening on port 443? – Tunji Dec 31 '16 at 17:21
  • No, I am using cloudflare free flexible ssl. So, I don't think I need that. – dev-jim Dec 31 '16 at 17:29
  • Is the ssl certificate installed on your server? or you're using it by turning it on from the cloudflare dashboard? – Tunji Dec 31 '16 at 17:34
  • I just turn it on on cloudflare dashboard. But I just found out I have a separate block that listen to port 443. So, I just remove this block? – dev-jim Dec 31 '16 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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