Update Page URLs on WordPress

I received a request to provide an estimate of what it would take to restructure a website. the website is based on Wordpress. I will be leaving the website on that platform, because the customer is already familiar with it and can make minor updates as necessary.

I have some Wordpress knowledge, so I know that the database could be updated with the new URLs for the various pages on the website. However, I wanted to check with ChatGPT on what it thought should be done. This is using the GPT-3.5 with some access to GPT4o.

GPT Response

Restructuring URLs on a WordPress website can be done in a few ways, depending on the specific needs and complexity of your site. Here are some common methods:

WordPress has built-in capabilities to change the structure of your URLs (permalinks):

Dashboard: Go to your WordPress dashboard. Settings: Navigate to "Settings" > "Permalinks". Choose Structure: Select one of the available permalink structures or create a custom structure. Save Changes: Click the "Save Changes" button to update the permalink structure. WordPress will automatically update your URLs and handle redirections for you.

Using a Plugin

If you need more advanced URL restructuring or want to handle redirections automatically, you can use plugins:

Redirection Plugin: Helps you manage 301 redirections and keep track of 404 errors. It’s useful for changing URLs and ensuring old URLs redirect to the new ones. Better Search Replace: Allows you to search for and replace URLs in your database, useful for bulk updating URL paths. Steps with Redirection Plugin:

Install Plugin: Install and activate the Redirection plugin. Create Redirects: Go to "Tools" > "Redirection" and add new redirects from your old URLs to the new ones. Monitor 404s: The plugin can also monitor 404 errors and help you set up redirects for those as needed.

Using SQL Queries

If you are comfortable with running SQL queries, you can perform bulk updates directly in the database. This method is more technical and should be done with caution.

Backup First: Always back up your database before running any SQL queries.

Example SQL Queries:

-- Update post URLs
UPDATE wp_posts SET post_name = REPLACE(post_name, 'old-part', 'new-part');

-- Update GUIDs (not always necessary)
UPDATE wp_posts SET guid = REPLACE(guid, 'http://oldsite.com', 'http://newsite.com');

-- Update URLs in post content
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://oldsite.com', 'http://newsite.com');

-- Update post meta
UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, 'http://oldsite.com', 'http://newsite.com');

-- Update options table (if URLs are stored there)
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://oldsite.com', 'http://newsite.com') WHERE option_name = 'home' OR option_name = 'siteurl';

Using .htaccess for Redirection

For simple redirections, you can add rules to your .htaccess file:

Redirect 301 /old-url/ http://yoursite.com/new-url/
Redirect 301 /another-old-url/ http://yoursite.com/another-new-url/

Programmatically via Functions.php

You can add redirection logic in your theme’s functions.php file using wp_redirect:

function custom_redirects() {
    if (is_page('old-page')) {
        wp_redirect(home_url('/new-page/'), 301);
add_action('template_redirect', 'custom_redirects');

Best Practices

Backup: Always back up your website and database before making any changes. Test Changes: Test your changes in a staging environment before applying them to your live site. Monitor: Use tools like Google Search Console to monitor for 404 errors and update redirects accordingly.

Using these methods, you can effectively restructure your URLs on a WordPress-based website. Choose the method that best fits your technical comfort level and the specific needs of your site.

My Thoughts

Based on the information provided, I am going to use a combination of updating the URLs in the database, and adding 301 redirects to the .htaccess file. The reason for the first, is so that if additional changes are needed in the future, that the built in functionality or WP plugins, can make the update. Better to use what is already available, than the reinvent the wheel.

Also, the potentional customer has provided direct links to a lof the content that is already on the site, and will not want to update those existing links to point to the new URLs. Thus using 301 redirects, will ensure that the content can still be accessed, after the restructing of the website. In addition, using 301 redirects, let's the search engines that crawl the website, know that the content has changed, and it can update itself faster than having to recrawl and reindex the entire website.

Posted: 2024-05-24
Author: Kenny Robinson