With Google getting picky about HTTPS on sites, I was hoping to be able to do a quick and easy SQL Query to search & replace anything http:// with https://
I normally do something like the below for moving hosts:
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
So I tried to do something like
UPDATE `wp_commentmeta` SET 'meta_value' = REPLACE(`meta_value`, 'http://', 'https://');
But it didn't seem to work. Anyway to do the ENTIRE database at once?
I have tried a few options on S.O.F., but nothing worked well.
I prefer not to have to install a plugin as a "fix"
If there is mySQL or htaccess script, I am more interested in those solutions.
If you have access to edit your .htaccess file, you can add the following into it:
RewriteEngine on
# force SSL
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
The code above will redirect with 301 (permanent), if don't want to use a 301 redirect, you can simply change the last section on the last line from [L,R=301] to [L,R].
If you want to be a little more thorough with your SQL replacement, you can usually find all the necessary links inside the posts table inside the guid column (featured images) and the post_content column (backlinks etc). And then ofcourse also inside the post_meta table - meta_value column and home/siteurl inside your options table. Here is the SQL Query that I normally use:
UPDATE wp_options SET option_value = replace(option_value, 'http://example.com', 'https://example.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = REPLACE (guid, 'http://example.com', 'https://example.com');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://example.com', 'https://example.com');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://example.com','https://example.com');
You can either try WP CLI or the Search Replace DB from Interconnect/it.
If you use the later, you can delete folder after replacing the URL.
You can try this free Wordpress plugin:
https://wordpress.org/plugins/better-search-replace/
Also checkout this article for other ways:
https://kinsta.com/knowledgebase/wordpress-search-and-replace/
Note that you should NOT replace GUIDs according to https://wordpress.org/support/article/changing-the-site-url/#important-guid-note:
the second part of that is that the GUID must never change. Even if you shift domains around, the post is still the same post, even in a new location. Feed readers being shifted to your new feeds when you change URLs should still know that they’ve read some of your posts before, and thus the GUID must remain unchanged.
Related
I use this commands to migrate in the old day, but with divi or elementor, some crash with migration, anyone use other way ? other idea ? It's due to Json ?
UPDATE wp_options
SET option_value = replace(option_value, 'http://www.old-site.com', 'https://www.new-site.com')
WHERE option_name = 'home'
OR option_name = 'siteurl';
UPDATE wp_posts
SET guid = REPLACE (guid, 'http://www.old-site.com', 'https://www.new-site');
UPDATE wp_posts
SET post_content = REPLACE (post_content, 'https://www.old-site.com', 'https://www.new-site.com');
UPDATE wp_postmeta
SET meta_value = REPLACE (meta_value, 'http://www.old-site.com','https://www.new-site.com');
Thanks
You shouldn't change the guid as that will cause RSS readers to think that all of the posts are now new unread posts.
You might be missing some stuff to replace. Typically when migrating to the new site you do the find and replace on everything in the database (except for the guid). There are some tools that can help you out with this. If you use wp-cli, you can use the wp search-replace command. Otherwise, this is a great tool. Both of these tools will automatically handle doing a proper search and replace inside of any serialized strings.
Also remember to clear out your cache.
My Situation:
I have url's in a field containing blog posts. The url's are being stored in my database with escape characters. My task at the moment is to replace some already inserted 'http' url's with 'https' url's, but REPLACE will match neither the original url nor the escaped url. I can't just replace every instance of 'http:', because I only want to affect certain links in each post, not every link.
I am very familiar with SQL, as well as REPLACE, so I'm not just asking how REPLACE works and how to use it. Another user here has tested my queries in his environment and they work. So, there must be something in my configuration that is preventing the queries from functioning as expected.
I have searched this site and google extensively for several hours and have found nothing specifically addressing my issue. Everything I have tried is included below and if there is something else I should try, I don't know what that is and I haven't found any suggestions/posts/comments that suggest doing anything differently.
Example URL:
http://test01.mysite.com
As Stored in DB:
http:\/\/test01.mysite.com
Code to Re-Create Situation:
DROP TABLE IF EXISTS test_posts;
CREATE TABLE IF NOT EXISTS test_posts (
id int NOT NULL AUTO_INCREMENT,
post_content longtext NOT NULL,
PRIMARY KEY (id)
)
INSERT INTO
test_posts
(post_content)
VALUES
('content content content Link I want to change content content content Link I don\'t want to change content content content Link I want to change content content content Link I don\'t want to change');
If I run
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http://test01.mysite.com', 'https://test01.mysite.com');
or
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com');
zero records are affected.
For testing purposes, I ran the following query which returns 0 rows.
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http://test01.mysite.com%'
OR
post_content LIKE '%http:\/\/test01.mysite.com%'
OR
post_content LIKE '%http:\\/\\/test01.mysite.com%'
OR
post_content LIKE 'http:%/%/test01.mysite.com%';
If I run:
SELECT
*
FROM
test_posts
WHERE
post_content LIKE '%http:_/_/test01.mysite.com%'
It does return matches, but that doesn't solve the real problem of how to match when using UPDATE/REPLACE.
I have tried on two different servers and I get the same results on both.
I have tried the following Engine/Collation combinations and all return the same 0 records results:
MyISAM/latin1_swedish_ci
MyISAM/utf8mb4_unicode_ci
InnoDB/latin1_swedish_ci
InnoDB/utf8mb4_unicode_ci
Anybody know how I can write these queries so that REPLACE will find matches to those url's or what settings in my database or PhpMyAdmin may be causing the queries to return/affect 0 rows?
I think the backslash must be escaped in MySQL
field_name LIKE 'http:\\/\\/test01.mysite.com%'
Of course one could go for sure and use the single char wildcard __
field_name LIKE 'http:_/_/test01.mysite.com%'
or for your both cases: an optional backslash:
field_name LIKE 'http:%/%/test01.mysite.com%'
I'm still baffled as to why the queries with LIKE won't work, but, sadly, using those to narrow down the problem clouded my judgement and I didn't try all the same combinations in the REPLACE functions.
The following works:
UPDATE
test_posts
SET
post_content = REPLACE(post_content, 'http:\\/\\/test01.mysite.com', 'https://test01.mysite.com');
If anyone can explain to me why these combinations work with REPLACE, but not with LIKE, I'd really love to know. Thanks!
There is no reason, your query won't work if you have run properly, there is something else, you may be missing here.
UPDATE
test1
SET
name_1 = REPLACE(name_1, 'http:\/\/test01.mysite.com', 'https://test01.mysite.com')
works well and does the job of repalcing the \/ with /.
See screen-shot attached,
You may have some other problem, please check and update the question, if so.
Edit after comments
If you have more data points in URL, change query like below.
UPDATE
test1
SET
name_1 = REPLACE(name_1, '\/', '/')
Above will replace all the occurrence of \/ with /.
As \\ did not work to represent/escape a backslash, use regular expression functions:
REGEXP_LIKE('.*http:\\/\\/test01\.mysite.com.*')
REGEXP_REPLACE(field, 'http:\\/\\/', 'http://')
Here \\ should work.
I am not a huge expert in MYSQL nor Regex (I guess I need to use some here), I would appreciate any help.
So my problem is that I would like to change some hardcoded URLs in Wordpress post contents to different ones.
Here is an example of a URL:
http://cdn1.domain.com/wp-content/uploads/2017/05/16211742/Screenshot_051117_101304_AM.jpg
Now, 'cdn1' can be cdn, cdn1 or cdn2. We might need to run 3 queries, but that is not a problem.
I would like to change all instances of this to:
//domain.com/wp-content/uploads/2017/05/16211742/Screenshot_051117_101304_AM.jpg
So basically remove the cdn (cdn, cdn1, cdn2) part, and remove the timestamp URL part before the filename.
The Wordpress database is something like this:
UPDATE wp_posts
SET post_content =
WHERE
Thank you very much!
First of all, take a database Backup for safety.
Then you can do this with SQL Command like this
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
OR you can do this with the plugin called: WP Migrate DB
There has a tutorial on the plugin page. check that.
Hope it will help you. :)
I'm working on a migrated WordPress, site url changed, everything works fine except some serialized data stored by a plugin.
The data is stored in the wp_options table, in the option_value column, for each record that the plugin saved.
So the data is still there, the problem is that when the url changed, it didnt get re-serialized (the count of the string content still thinks it's the old url lenght), therefore the plugin isn't working properly.
So, to find exactly the records that need to be updated, I use
$t1 = $wpdb->prefix . "vslider";
$t2 = $wpdb->prefix . "options";
$records_ineed = $wpdb->get_results("SELECT * FROM '".$t1."', '".$t2."' WHERE '".$t1."'.option_name='".$t2."'.option_name");
That gives me exactly the records that I need to re-serialize (it matches the name of the record created in the plugin table, with the record created in the wp_option table)
What do I do now?!
How do I take only the option_value of each, unserialize it, reserialize it and update the existing value in the db?
Should I save the reserialized values to a new table, and then replace from that table back to wp_options? How?
Otherwise what are other solutions?
There is no need to do that , what you need is to import the value as - is . it is an array and it will work fine as such as long as you do not alter the data in any way.
At any event , when you import migrate a wp site to another URL , there is absolutely no need to read the wp-options table´s columns one-by-one .
What you need to do it just dump your SQL from the Original ( old-domain ) site , then import into the new-domain , and then run these queries :
/**
To update WordPress options with the new blog location, use the following SQL command:
**/
UPDATE wp_options SET option_value = replace(option_value, 'http://www.old-domain.com', 'http://www.new-domain.com') WHERE option_name = 'home' OR option_name = 'siteurl';
/**
After that you will need to fix URLs of the WordPress posts and pages, which translated from post slug, and stored in database wp_posts table as guid field. The URL values in this field are stored as abolute URLs instead of relative URLs, so it needs to be changed with the following SQL query:
**/
UPDATE wp_posts SET guid = replace(guid, 'http://www.old-domain.com','http://www.new-domain.com');
/**
If you have linked internally within blog posts or pages with absolute URLs, these links will point to wrong locations after you move the blog location. Use the following SQL commands to fix all internal links to own blog in all WordPress posts and pages:
**/
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old-domain.com', 'http://www.new-domain.com');
Just for verification , after running those commands , go back to the options table and verify that your home and site_url functions are both correct as for the new-domain data.
If you do still want , for some obscure reason, insert data directly into the options table manually ( ?? why ) then you should consider using get_post_meta() and update_post_meta() function which will take care of the serialization.
I have a wordpress site that I moved from domain.info to domain.com. Some of the older articles still reference images at domain.info. How can I script it to replace all instances of example.info with example.com? Can I do it in MySQL without having SSH access to find and replace?
http://www.example.info/wp-content/uploads/2009/09/image-239.jpg
should be
http://www.example.com/wp-content/uploads/2009/09/image-239.jpg
EDIT:
Or maybe even using .htaccess?
UPDATE mytable SET url = REPLACE(url, 'www.example.info', 'www.example.com')
WHERE url LIKE 'http://www.example.info%'
or somewhat slower:
UPDATE mytable SET url = REPLACE(url, 'www.example.info', 'www.example.com')
WHERE url LIKE '%www.example.info%'
See:
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace
I would think that using a 301 (redirect) would be better, so you don't have SEO issues and lose any synergy you may already have.