Enable https access for the entire site: Apache+WordPress deploys SSL modification and 301 redirect

888u

Last update at :2024-01-01,Edit by888u

With major search engines supporting SSL https, more and more sites are enabling SSL https mode. This site was activated once before, then canceled, and now it is activated again. As for the SSL certificate, you can choose the free one from Alibaba or Tencent. Of course, you can also choose to pay for it by spending a little money. I won’t go into detail here about how to configure SSL. Commercial SSL merchants generally provide corresponding installation tutorials or related guidance.

What this site wants to explain below is some follow-up work when using the wordpress program after enabling SSL, which is also a necessary work that has to be done. This site uses a LAMP environment, so the following 301 redirect is suitable for the Apache environment.

1. Enable https access and domain name 301 redirection

Use the .htaccess file to implement 301 redirection. Edit the .htaccess in the root directory of the website and add the following code:

  1. RewriteCond %{HTTPS} !on [NC]
  2. RewriteRule (.*) https://www.thissite.com%{REQUEST_URI} [R=301,NC,L]

Note: Please change the domain name link in the article to your own!

2. Force SSL to be turned on during login and background

Modify the WP-config.php file and add the following two lines of code directly at the end of the file:

  1. define(‘FORCE_SSL_LOGIN’, true);
  2. define(‘FORCE_SSL_ADMIN’, true);

3. Let the links within the site support SSL

Attachments uploaded to the space are marked as absolute links by WordPress. Generally, the database needs to be modified, but this method is more dangerous, so another method is recommended

Code method, edit the function.php file under the current theme and add the following code:

  1. /* Replace the image link with https */
  2. function my_content_manipulator($content){
  3. if( is_ssl() ){
  4. $content = str_replace('http://xxx.com/wp-content/uploads', 'https://xxx.com/wp-content/uploads', $content);
  5. }
  6. return $content;
  7. }
  8. add_filter(‘the_content’, ‘my_content_manipulator’);

If you modify the database directly, you can use the following method:

URLs such as website homepage and website articles, some links in articles and picture URLs are still old URLs. If we want to update these URL addresses, we need to manually operate the mysql database and execute the following sql statements in sequence: < /p>

UPDATE wp_posts SET post_content = replace( post_content, 'http://www.this site.com','https://www.this site.com');
UPDATE wp_comments SET comment_content = replace(comment_content, 'http://www.this site.com','https://www.this site.com');
UPDATE wp_comments SET comment_author_url = replace(comment_author_url, 'http://www.this site.com','https://www.this site.com');

Note: http://www.this site.com in the sql execution statement refers to the old domain name, while https://www.this site.com refers to your new domain name, which should be changed to Just make your own!

Finally, modify the "Site Address" and "WordPress Address" in "Settings" → "General" to HTTPS!

Recommended site searches: in domain name, Korean proxy IP, renting a server, website registration number query, space registration, overseas server, US vps host, overseas server rental price, domain name query, high-defense server rental,

Enable https access for the entire site: Apache+WordPress deploys SSL modification and 301 redirect

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码