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?

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/'
  • Super good call! I didn't even think of this. Giving it a shot now. – Josh Habdas Aug 14 '17 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! – Josh Habdas Aug 14 '17 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 Aug 18 '17 at 10:22
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

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.

  • 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... – Josh Habdas Aug 14 '17 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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