What is the use of WordPress image secondary domain name?How to change to image subdomain

When you use a subdomain (second-level domain name) as a category directory or topic, you can increase the weight of the URL to achieveSEOThe effect of search engine optimization.

For example, all images on the website, use the second-level domain name img.chenweiliang.com As a picture bed ▼

What is the use of WordPress image secondary domain name?How to change to image subdomain

You can use the secondary domain name as an image path, transfer images from your blog to faster hosting at any time, just upload the image and change the subdomain resolution, and the problem will be solved.

If you have a domestic CDN service host, you can also greatly speed up browsing and reduce the burden on many servers.

due toChen WeiliangBlogs use WWW subdomains. Cookies of this type of subdomain will not pollute the IMG subdomain, and you can enjoy cookie-free and speed up access.

What is cookie-free?

YSlow offers 22 tips on how to improve web page efficiency and optimize website performance.

  • One of them is about domain names: use cookie-free domains.
  • When a user's browser sends a static file (such as a picture image or CSS style sheet file), the cookies in the same domain name (or second-level domain name) will be sent at the same time, but the web server completely ignores the sent cookies, so these useless cookies are Waste website bandwidth, affect website acceleration and web page performance.
  • YSlow suggests that to solve this problem, you can use the Cookie-free domains approach to optimize the use of cookies to improve the efficiency of your web pages.

If you use directly like chenweiliang.com Such a top-level domain name is used as your blog domain name, so using a subdomain name as a picture bed cannot achieve Cookie-free.

  • because the top-level domain chenweiliang.com A cookie is sent to the secondary nameservers for all requested static files.

If you want to support cookie-free image beds, you need to use a different domain name to achieve cookie-free.

  • Chen WeiliangBlog use www.chenweiliang.com 's subdomain is fine.

Add the specified cookies domain

In the wp-config.php file, add the following statement ▼

/** 指定cookies域 */
define('COOKIE_DOMAIN', 'www.chenweiliang.com');

The following isWordpress Official description of Set Cookie Domain:

The COOKIES Domain set for Wordpress can be used for some special domain name settings.For example, using a second-level domain name to store static content.To prevent Wordpress cookies from being sent on every request for static content on a second-level domain, we can just set the non-static domain to cookie domian.

The domain set in the cookies for WordPress can be specified for those with unusual domain setups. One reason is if subdomains are used to serve static content. To prevent WordPress cookies from being sent with each request to static content on your subdomain you can set the cookie domain to your non -static domain only.

resolve subdomains

Step 1:Enter DNSPod domain name management, add second-level domain name (subdomain name) ▼

Step 2:Resolve the A record of the subdomain to the IP address of the host▼

Enter the DNSPOD domain name panel, resolve the A record of the subdomain name to the third IP address of the host

Step 3:Add a second-level domain name on the hosting panel

  • No, please ask your domain name or hosting provider.

VestaCPTo add a domain name to the panel, you can refer to this tutorial▼

Copy the image to the second-level domain name directory

After a subdomain is bound, a directory is usually created automatically containing the subdomain as the directory name.

For example:

  • If you bind img.chenweiliang.com, the IMG directory will be automatically generated.
  • If it is a WordPress blog, please wp-content/uploads The files in the directory are copied to the IMG directory.

The following is an example of the server path of the VestaCP panel (please modify it to your own server path).

Step 1:SSH into the uploads folder of WordPress ▼

cd /home/用户名/web/你的域名/public_html/wp-content/uploads

Step 2:Copy all files in the current directory to the specified directory ▼

cp -rpf -f * /home/用户名/web/图片二级域名/public_html/

Step 3:Repair image secondary domain name authority ▼

chown -R admin:admin /home/用户名/web/图片二级域名/public_html/*

WordPress set file upload path

WordPress version 3.5 or later hides the upload path (upload_path) and file URL address (upload_url_path) settings of the Media settings page in the background.

The picture below is the previous version of the media settings interface ▼

What is the use of WordPress image secondary domain name?How to change to image subdomain

  • With the settings here, you can customize the location where the file is saved and the generated address.
  • This feature is pretty good, just don't know why it should be hidden?

If you still need to customize, you can try the following methods to restore.

Simply add the following code to your WP theme's functions.php file to restore the settings interface:

//找回上传设置
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}
}
  • This method is the simplest and most effective, so it is recommended.

Since the img directory is still on the current host, you can still use the editor that comes with WordPress to upload and add images when writing blog posts.

Modify the WordPress image upload path

Step 1:go to media options

Click on "Media" under "Settings" ▼

What is the use of WordPress image secondary domain name?How to change to image subdomain

Step 2:Change the "default upload path" to the server path of the IMG directory ▼

/home/用户名/web/img.chenweiliang.com/public_html
  •  Note that there should be no "/" after it.

Step 3:Change the "full URL of the file" to the second-level domain name of the image ▼

https://img.chenweiliang.com
  • Note that there should be no "/" after it.

Step 4:Click "Save Changes".

Replace image path in database

The following is an example of the server path of the VestaCP panel (please modify it to your own server path).

replaceMySQL databaseIn the path, it is recommended to install and use the WP Migrate DB plugin ▼

Step 1:Database batch replaces the default upload path

Change the original server path ▼

/home/用户名/web/chenweiliang.com/public_html/wp-content/uploads

Replace with new server path ▼

/home/用户名/web/img.chenweiliang.com/public_html

Step 2:Database batch replacement image secondary domain name

Convert the original image URL ▼

https://www. 你的域名 .com /wp-content/uploads/
  • Note: Spaces are added to the above URL to avoid dead links in this article.

Replace with a new image second-level domain name ▼

https://img. 你的域名 .com/
  • Note: Spaces are added to the above URL to avoid dead links in this article.

Image link 301 redirect

Instructions for 301 redirects with regular expressions in the .htaccess file:

  • (.+) corresponds to any character (including Chinese characters, English letters, etc.)
  • (\d+) corresponds to any number (only Arabic numbers)
  • $1 $2 $3 is a re-reference of the variable that appeared earlier

You can use RedirectMatch to achieve link redirection:

  • will:https://www. 你的域名 .com/wp-content/uploads/
  • redirect to:https://img. 你的域名 .com/

In the .htaccess file, add the following 301 redirect code ▼

RedirectMatch 301 ^/wp-content/uploads/(.*)$ https://img.chenweiliang.com/$1

Delete the original picture directory

Step 1:SSH into the uploads folder of WordPress ▼

cd /home/用户名/web/你的域名/public_html/wp-content/

Step 2:Delete the uploads folder directory ▼

rm -rf uploads
  • If the uploads folder directory is not deleted, 301 redirection to the image second-level domain name may not be successful.

Check the modification result

  1. Check and refresh the article page to see if the image is displayed as usual?
  2. Check the image path, is it the image path of the new second-level domain name?
  3. Check the original image URL, whether 301 is successfully redirected to the image URL of the second-level domain name?
  4. Go to the WordPress post editor and check the post image display, is it displayed as usual?

If everything goes as usual, you have completed the setup of the secondary domain name for WordPress image loading.

  • Images in future articles will be saved in the IMG directory.

When you need to move your website to transfer pictures, just pack the IMG directory and upload it to the new host.

  • Then, modify the IP address of the img.chenweiliang.com second-level domain name in DNSPod.

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "What is the use of WordPress image secondary domain name?How to Change to Image Subdomain", it will help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-749.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top