1

I'm wrapping up a port from Jekyll to WordPress and have several thousand relative URLs I need to remap so they're absolute within my posts.

Actual URL examples:

/gangs/gangster-disciples
/housing-projects/cabrini-green
/hoods/bridgeport

Expected URL samples:

http://example.com/gang/gangster-disciples
http://example.com/project/cabrini-green
http://example.com/neighborhood/bridgeport

Dax isn't turning up anything useful based on my search phrases and I'm not able to find any plugins or similar questions to study.

Short of writing some update queries I'm curious to know if there's a more streamlined solution available I may be overlooking, such as a plugin-based solution. What's the easiest way to remap the example relative URLs to absolute?

3 Answers 3

1

WP CLI is a great tool for performing common admin and maintenance tasks on a WordPress install. It has a large range of commands that among other things allow you to install plugins, regenerate thumbnails, and in your case, perform a search and replace on the database.

When migrating from dev to production I'd normally run the following command to update URLs in the database:

wp search-replace '//dev.example.com/' '//www.example.com/'

With your existing URL structure you'll probably need to include the href=" in the search and replace string like so:

wp search-replace 'href="/' 'href="http://example.com/'
5
  • Super good call! I didn't even think of this. Giving it a shot now.
    – vhs
    Commented Aug 14, 2017 at 12:50
  • I'm selecting this as the correct answer because, while the plugin approach may be more friendly for some, the CLI approach does pretty much all of the same stuff (sql exports, dry runs) and is scriptable. Awesome!
    – vhs
    Commented Aug 14, 2017 at 15:28
  • Glad I could help. I figured if you'd been using Jekyll you'd like using the command line :) I find WP CLI indispensable.
    – Dylan
    Commented Aug 18, 2017 at 10:22
  • Does it check and handle data serialization? Because if not, it will brick your site instantly. Commented Dec 20, 2020 at 18:46
  • It handles serialisation... it wouldn't be very helpful if it didn't :)
    – Dylan
    Commented Jan 14, 2021 at 0:49
2

There are several plugin solutions such as Search and Replace which will "bulkly" update almost whatever you want in DB. This one has a "Replace a Domain / Url” special feature:

Useful for a quick and simple transfer or a migration of an WordPress.

1
  • Found a similar plugin from Delicious Brains called WP Migrate DB which is intended for this kind of operation and also handles updating GUIDs if desired.
    – vhs
    Commented Aug 20, 2017 at 10:36
1

If you switched from a different system to WP, first you would need a proper redirection set up - either manually in .htaccess, or using a plugin like Simple 301 Redirects.

This would help the search engines find your pages, and it would also take care of your links - at least for starters.

After you're done with this step, if you need simple search and replace, the plugin Search and Replace (already mentioned by ClemC in his answer) is good enough for that. But if you need preg_replace(), there is no WP plugin offering that, as far as I know. Still, there is a tool offering preg_replace(): Search Replace DB. Since the tool offers no authentication, it could be used by anybody who could guess the url of the script. As a precaution, I would use this method to install the script: create a folder with a secret name, protect it with a password using .htaccess, then copy the script to that folder. After you're done with it, delete the script from the server. Don't leave it unprotected on your server, as versions of this script are targeted by hackers, causing serious problems to WP users.

1
  • I like the power preg_replace but I've read some horror stories about use of regex against HTML documents here on the Stacks. I guess what I really want is to get Atom connected to the DB tables and use the find and replace feature. Hmm...
    – vhs
    Commented Aug 14, 2017 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.