© EMBAKAITECH

0 %
EmbakaiTech
Full-Stack Developer
Ui/UX Designer
Pidgin
English
html
CSS
Js
PHP
WordPress
  • Bootstrap, Materialize
  • Stylus, Sass, Less
  • Gulp, Webpack, Grunt
  • GIT knowledge

How to change your WordPress directory

December 13, 2023

Many WordPress users prefer to create their site in a separate directory to prevent others from seeing it until it’s ready. After completing development, you may want to move your WordPress to the root directory (public_html). You might want to change the directory name to improve security or to better organize your files. Whatever the reason, a CMS like WordPress allows you to maneuver the location of its files.

Here, we will explain how to change your WordPress site’s directory with minimal downtime and no data loss. Read on for step-by-step instructions.

Why would I change the directory structure?

You may want to change the WordPress directory for your site in any of the following scenarios:

  • If you are launching a new website but want to keep it running with the same URL as your old site, you need to reconfigure WordPress and change its directory.
  • You didn’t want your site to be visible during development so created it in a subdirectory (like yourdomain.com/blog).  When your WordPress website is ready to go live – you’ll need to move it to your account’s public_html. This ensures that your site shows as “yourdomain.com” instead of “yourdomain.com/blog.”
  • Let’s say you created your site in a subfolder like yourdomain.com/wordpress, and you want to change it to yourwebsite.com/blog. Then, you need to reconfigure your WordPress and change its folder name.
  • You may want to keep your WordPress files outside the root folder for security reasons. Hackers can easily find your file structure out and target specific files.
  • Lastly, you may want to keep your public_html neatly organized. Placing your WordPress installation in a subfolder while it loads with yourdomain.com is an excellent way to do that.

How do I change the directory of my WordPress installation

Safety Precautions

It’s no secret that having a backup of your WordPress website is vital for your website’s security. A backup of your site’s latest version is always helpful when things don’t go according to plan. Therefore, we recommend that you generate a site backup before initiating any changes to your WordPress directory.

As another safety precaution, you might want to enable debug mode for your WordPress. Activating this mode prevents error messages from being displayed on your site. Often, errors on your front end may disclose sensitive information about your file structure.

With debug mode on, any PHP errors are logged in a debug.log file instead of on your site.

Lastly, you may consider creating a 302 Redirect for your site while performing this structure change. This way, your visitors and search engines (like Google) know that your site will be back online shortly.

How to change the directory of WordPress manually?

In this case, we’ll use the example of moving your site from the public_html to a subdirectory – from yourdomain.com to yourdomain.com/blog. In this instance, we’ll still have WordPress working with the same URL (yourdomain.com).

Consider this as a security measure that obscures your file structure in order to trick hackers. To maximize your WordPress security you should also regularly scan your site for malware infections.

Or, you may want a neater-looking root folder, and changing your WordPress directory can help with that.

To force WordPress to change its folder name, you have to adjust several settings. Read below for a step-by-step guide to performing this task.

Step 1. Create a new directory

You should start by creating a new directory if you want to move your site from the root folder to a subdirectory. That folder will be your WordPress files’ new location, so choose its name according to your preferences.

To create a new directory, you can use your hosting account’s File Manager, an FTP client, or an SSH connection.

Step 2Edit your WordPress URL from the Dashboard

When you move your site from the public_html to a subdirectory, your site URL will have to change from yourdomain.com to yourdomain.com/blog. In this case, we want to keep the same URL but the files in a subfolder. Therefore, this needs to be adjusted in the WordPress Dashboard.

To do this, log into your WordPress Dashboard and navigate to the Settings > General section. In the field WordPress Address (URL), you need to define the directory where your site’s core files are located. While in the Site Address (URL) field, type in the URL you want the site to load with.

When you are ready, click Save Changes, and WordPress will log you out of the Dashboard.

Note that your site will be inaccessible until you complete the process. But don’t worry, when you have finished all the necessary steps, your site will be up and running again.

If you are working with a WordPress Multisite, you won’t be able to edit the site URL from the Super Admin Dashboard. This needs to be done from the wp-config.php file of your WordPress site.

To do that, open the wp-config.php file and paste the following snippet before the line that says

				
					/* That's all, stop editing! Happy publishing. */
define('WP_SITEURL', 'https://' . $_SERVER['SERVER_NAME'] . '/blog');

define('WP_HOME', 'https://' . $_SERVER['SERVER_NAME']);
				
			
				
					Replace the ‘/blog’ part depending on the actual name of the folder you’ll be moving your WordPress to and save the changes.
				
			

Step 3. Move the files from the original directory to the new one

You can do this step via an FTP client, SSH connection, or the File Manager tool in your hosting control panel. Here we will outline the File Manager approach, so open the tool to load the public_html. There, you have to select all your WordPress files and folders and move them to the new subdirectory.

Step 4. Edit the .htaccess file

After all files have been moved, you will need to edit the .htaccess file to define the new path to the installation. If you haven’t moved that particular file to the new subdirectory, you can create a new one. Copy the code below and paste it into the .htaccess.

 

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

Note that you need to replace ‘yourdomain.com’ with your domain and ‘blog’ with the actual name of your subdirectory.

Step 5. Copy the index.php and .htaccess to the root directory

To make your site load the content from your subdirectory without it showing in the URL, copy the .htaccess and index.php to the root directory. Make sure that you are copying these files and not moving them, as they need to be present in both folders.

You need to edit the index.php in the public_html and add the name of the subfolder where your WordPress files are. To define the directory in the root index.php, add its name right before “‘/wp-blog-header.php’;” as shown below.

Step 6. Update your WordPress Permalinks

If your internal pages show a 404 error, you need to refresh your Permalinks settings. Keep in mind that when you go to yourdomain.com/wp-admin/, your browser will redirect you to yourdomain.com/blog/wp-admin/. However, the URL in the address bar will remain the same for a visitor.

When you are in your Dashboard, go to Settings > Permalinks and check whether your URL structure is correct. If everything is ok, click Save Changes to refresh your Permalinks.

That updates your .htaccess file according to the new file structure and should fix any 404 errors on the internal pages of your site.

Occasionally, your posts may not show correctly with the new URL. To fix this, you should update your Permalinks structure. Select the Custom Structure and add your new directory name to do this.

Save the changes and flush your browser cache to ensure that you are seeing the latest version of your site.

Good job! Now your WordPress files load from a subdirectory while your URL remains the same.

To sum up, you can follow the steps above to change your WordPress directory name.

Posted in How-To, WordPress
Write a comment