Easy Migrating of WordPress Site’s URL to Root URL, with Program Files Remaining in Subdirectory 🏖

Picture of volkswagon bus in desert with big rocks.

I built a site for a client on her server under the url ‘https://mysite.com/wordpress’, but then for production I had to change the url to remove the ‘/wordpress’. The wordpress files on the server were in the directory ‘wordpress’, which was in the root directory, ‘public_html’. So the files were in ‘public_html/wordpress’.

I want to say that one smart idea when making changes to WordPress is to disable W3Cache, or other database caching plugins. These can prevent browsers from showing you the real-time updates, and can lead to lots of wasted time and confusion.

The first thing to do when migrating a WordPress site is to back up all of the files and the database. I failed to backup my WordPress files. Like Andy Dufresne, I came out clean in the end (I think), but it would have been an excellent idea to have a backup. This can save lots of headache when you edit things incorrectly, or forget what you changed. The database can be exported from phpMyAdmin in a matter of seconds, but the WordPress files may take several hours to download through FTP.

Changing the Site Address in WordPress Dashboard 🏡

One of the instructions i found online told me to change the WordPress site address, so I did. This was done through the WordPress Dashboard: (Dashboard Home)>Settings>General>’Site Address (URL)’. The Site Address field tells you ‘Enter the address here if you want your site home page to be different from your WordPress installation directory.’ Be careful NOT to edit ‘WordPress URL’ (unless you know what you’re doing), because it will be difficult to be able to get to your dashboard again if you have not done the proper configuration.

My site address url was ‘https://mydomain.com/wordpress’, and I changed this to ‘https://mydomain.com’. This would cause a 404 error for all of the sites pages, which would be fixed with the .htaccess file change, described below.

Redirecting URL Requests with .htaccess File in Root Folder 👻

I copied the ‘wp-config’ file and the ‘index.php’ file to the root directory, as per recommendations I found on the web. Then, I followed the instructions here to create a new .htaccess file for the root directory (‘public_html’ in my case). I used Method I, which was meant for use if you’re not changing the url, but for some reason I used this code anyway and it eventually worked:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
</IfModule>


There is also a method on the instruction page with shorter code, that is supposed to be used when changing the url.

White Screen of Death ☠️

At some point in my process of migration, none of the pages were working anymore – showing only a totally blank white or grey screen, known as the ‘WordPress white screen of death’ – and the console was displaying a message like ‘net::error cannot load content’. 🤯 This was a scary problem, but it was solved by searching the web and finding the solution of deactivating all of my installed plugins, and trying again. This made the pages work again, so I reactivated the plugins one-by-one, and found that the W3Cache plugin was breaking the site. I couldn’t figure out a solution, even after deleting the plugin and all of it’s residual files, and then reinstalling, so I just scrapped it. It wasn’t worth it for me to spend time on a solution for this.

Fixing Broken Image URLs 🌃

The urls for my images broke after this migration. When I inspected the image urls, they showed ‘https://mydomain.comwp-content/…’. There were two problems with this: First the trailing slash was missing after ‘.com’; and second, the wp-content folder was not in the root directory, as all the WordPress files had been kept in my WordPress folder.

I backed up my database before making any edits, which are described below.

I went to my WordPress database on phpMyAdmin through cPanel to check that the urls were indeed incorrect. I had trouble finding my images, but one thing I was doing wrong was leaving the database search results to only 25 items, and thinking that this was everything. I ended up realizing that I could just search ‘images’ and the relevant data would turn up. I could see that the urls were incorrect here too. I used the WordPress Velvet Blues Update URLs plugin to change the urls of these files, using the inputs ‘Old Url: https://mydomain.comwp-content’ and ‘New Url: https://mydomain.com/wordpress/wp-content’. I believe this just executes some ‘find-and-replace’ SQL code to change the name of everything that contains ‘Old Url’ (even all the files and subfolders inside ‘wp-content’, for example ‘/wp-content/cache’ and all of it’s contents would have their urls changed from this plugin command). So this could probably be done manually in phpMyAdmin as well with SQL code.

Completion

Spoiler Alert: This wasn’t actually easy for me, but I hope it will be for you! 😘

Eventually all of this got working, but this was a 6-hour process of trial-and error for me. I also in the middle of failing at this process tried to move all of the files to the root directory, which caused me more issues that were hard to solve. However, the internet has lots of great resources, and there’s always a solution to be found in web development. The computers, servers, hosting services, etc. that we all use are different, and can often require a grouping of solutions and trial-and-error from different sources. Putting these together to solve your problem is a fun challenge, an exciting puzzle, and is what web development is all about!

The conclusion was that eventually I solved the problem, with the relevant program files remaining in the ‘wordpress’ subdirectory. Victory Screech!!! 👨‍💻

Picture of guy at festival with his hands up.
@veronicacarswell

Volkswagon bus image.

Continuation: Log for Migration Attempt #2, 18 Dec 2020, 12:00 am

Tried to migrate sandbox update to root URL. Was not able to do it. I changed htaccess file, and then browser started redirecting to sandbox url, but wouldn’t stop redirecting even when I changed htaccess back. The browser apparantly stores information about where to redirect to, so I had to open in Firefox, and then the cache got saved there too, so I did it in incognito browser, and this seemed to be working.

After just changing “Site URL” in general settings, and adding htaccess file to root to direct to sandbox folder, at first I got a 404 error on the homepage, but after waiting a few minutes, the homepage now appeared on root url. However, now issue was that all subpages return white screen of death. As of 12:15 am, have not solved this issue. Will leave it until tomorrow.

Note to self and others: AVOID creating versions of WordPress sites that you plan to migrate their URL later. Modify the site at the root instead. Migrating is a huge pain in the ass waste of time, and should definitely be avoided at all costs.

(18 Dec 2020, 8:05 am)

I tried deactivating all plugins to solve the white screen of death for all my subpages, but this did not work. I am now going to try to use the debug log.

Following instructions from How to Fix the WordPress White Screen of Death at WPBeginner: https://www.wpbeginner.com/wp-tutorials/how-to-fix-the-wordpress-white-screen-of-death/

Adding following PHP code to wp-config.php file:

define( 'WP_DEBUG', true);
define( 'WP_DEBUG_LOG', true );

(18 Dec 2020, 8:05 am)

Finally fixed the problem by just moving all of the subdirectory files into the root directory, and changing the “Site Address” and “WordPress Address” to root in general settings.