How can I redirect to https when using Wordpress?

I am trying to use .htaccess to redirect al http: // pages to https: //. I can do this with regular pages without issue using the codes available on the Apache site, but I am having issues with the same when using WordPress.

I tried the question here but it didn't work for me.

Here is the code I'm using:

# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

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

      

and I get this message:

The webpage at https://example.com/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

      

I also tried this answer on Stack Overflow with the same result. I obviously cleared the cache and cookies to see if this was the problem, tried different browsers and even different computers as far as I could modify the code, but I just gave up, so I'm looking for help.

What am I doing wrong here? Any help is greatly appreciated

EDIT: While I was solving the problem, I found that there is an additional problem that I was not aware of and that was the main problem. The site owner installed the WordPress HTTPS Plugin without my knowledge and I didn't notice it (my bad one). This is added to the DB changes I had to make and some persistent cache issues make the site go berserk.

So this is what I did:

  • 1) In settings> General> Address (URL) WordPress changed http ... address to https
  • 2) In the section "Settings"> "General"> "Site address" (URL), the address http ... has changed to https
  • 3) Used lxg suggestion
  • 4) Remote WordPress HTTPS Plugin
  • 5) Created Permalinks again (just to create a new .htaccess file)

Bam! Works like a charm.

Just a warning: it broke the site because I was serving files from a CDN like JQuery, Bootstrap, etc., so they were rejected as secure connections. I just needed to put the files on my server and now everything works as expected.

Hope this helps someone, I'm sure I'm almost crazy!

+3


source to share


2 answers


The problem is that WordPress also stores the base URLs in the database and tries to redirect them. You must update these fields as well.

This is usually done in the administration area, but when a redirect is active, you usually cannot achieve it. Therefore, you can do it with a simple DB query.

Check the old values โ€‹โ€‹first ... in case something goes wrong. (Note: I'm assuming your table prefix is โ€‹โ€‹the default wp_

.)

SELECT * 
FROM wp_options
WHERE option_name IN ('home', 'siteurl');

      

This should give you something like:

+-----------+-------------+------------------------+----------+
| option_id | option_name | option_value           | autoload |
+-----------+-------------+------------------------+----------+
|        37 | home        | http://www.example.com | yes      |
|         1 | siteurl     | http://www.example.com | yes      |
+-----------+-------------+------------------------+----------+

      



If both option_value

are equal (they are found in 99% of all WP installations), you can simply update them to the new url with a single request, as shown below. If not, update them separately.

UPDATE wp_options
SET option_value = 'https://www.example.com' 
WHERE option_name IN ('home', 'siteurl');

      

Now you need to install WP using the new URL.

Another problem is that posts or other settings (like plugins) contain the old url.

This should be handled correctly by WordPress, but: (a) every call to the old url is an unnecessary call to the web server, and (b) it will redirect the user to use an unsecured url.

There is no simple hack for this problem, but you have to search and replace in your DB. I have had good (but not always perfect) results with DB search / replace . Make sure to back up your DB when using it - although it's a stable tool, everything it does is error prone.

+1


source


You can also use ssl-insecure-content-fixer to fix any jquery errors. If set in a tiered setting must be individually activated on each subdomain.



0


source







All Articles