To change from HTTP to HTTPS on a WordPress site without plugin, you can follow these steps.
Before you begin, make sure to create a backup of your site.
Install SSL
Contact your hosting provider and request the installation of an SSL certificate for your domain. Many hosting providers offer free SSL certificates through services like Let’s Encrypt.
Change WordPress Address (URL) and Site Address (URL) in the Admin Panel:
Go to the WordPress admin panel.
Navigate to “Settings” -> “General.”
Change both “WordPress Address (URL)” and “Site Address (URL)” from HTTP to HTTPS.
Update Links in Content
If you switched to HTTPS after creating content, there might be links in post content that still use HTTP. Use an SQL query to update them in the database. This can be done using a tool like phpMyAdmin. Be cautious when executing SQL queries and make a backup first.
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://', 'https://');
Redirect from HTTP to HTTPS
To automatically redirect users from HTTP to HTTPS, you can add the following code to your .htaccess
file:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </IfModule>
Redirect WordPress Pages from HTTP to HTTPS with PHP code
Add this code in the function.php file. And always back up. All kinds of problems are possible, so you need to know what you’re doing and what to expect.
The code currently only displays the changes and does not write them to the database.
function replace_http_with_https($content) {
if (is_ssl()) {
$content = str_replace('http://', 'https://', $content);
}
return $content;
}
add_filter('the_content', 'replace_http_with_https');
add_filter('widget_text_content', 'replace_http_with_https');
If you want to make permanent changes to the database, you can use the following code.
function replace_http_with_https($content) {
if (is_ssl()) {
$content = str_replace('http://', 'https://', $content);
}
return $content;
}
function replace_http_with_https_in_database() {
global $wpdb;
// Replace HTTP with HTTPS in all posts and pages
$wpdb->query("UPDATE $wpdb->posts SET post_content = REPLACE(post_content, 'http://', 'https://')");
}
// Replace in the content of posts and pages when the 'save_post' hook is triggered
add_filter('the_content', 'replace_http_with_https');
add_filter('widget_text_content', 'replace_http_with_https');
add_action('save_post', 'replace_http_with_https_in_database');
If you are using Nginx, add a similar code snippet to the server’s configuration file.
Carefully check your site to ensure everything is working correctly. Pay attention to any issues with content, links, and forms.
After completing these steps, your site should be functioning with HTTPS.