Using WP-CLI to search-replace and Remove .html from End of URLs

Scenario: we were migrating a WordPress site from one domain to another. The blog posts all ended in .html which we wanted to remove for cleanliness and SEO. We created this code in WPENGINE to handle the redirects for any links to the old content:

Domain: the URL of the old domain
Source: ^(.+)\.html$
Destination: /$1

However, this only took care of requests to the previous domain – there was still interlinking within the posts that was pointing to defunct URLs ending in .html. So we wanted to bulk replace all the URLs within the content to be correct.

To remove that I used the below command in WP-CLI to search-replace and remove just the .html ending and only on the blog URLs (So it wouldn’t affect say an external link to another blog that was using .html as an ending.)

wp search-replace '/blog/(.+)\.html' '/blog/$1/' wp_posts wp_yoast_seo_links --network --regex

Notes:

Exit mobile version