How to move WordPress from a subdirectory to the root of your domain
Two jobs share this name. To show the site at the root while its files stay in the folder, change "Site Address (URL)" and copy index.php and .htaccess to the root. To remove the folder, change both addresses, move the files, replace the old address in the database and redirect the old folder.
- By
- WP Ministry
- Published
In short
- Two jobs share this name. One leaves the files in the folder and only shows the site at the root. The other moves the files and changes every address.
- Leaving the files in place is the smaller job and is easy to undo.
- When the files do move, change the two addresses first and move the files second.
- After a full move, replace the folder's address in the database with a tool that understands serialized data.
- Redirect each old address to the same page at the root with a 301, and keep the redirect for at least a year.
- If the dashboard will not load, two lines in wp-config.php get you back in.
A site at example.com/wordpress/ can be made to answer at example.com in two ways. You can leave the files in the folder and change only where the site answers. Or you can move the files to the root and change every address the site has.
The first is the smaller job and is easy to undo. The second leaves nothing behind in the folder, but the dashboard and every image get a new address, so the database has to be updated too. Either way, old links into the folder have to keep working. The steps here are for a single site, not a multisite network.
Which of the two jobs you have
| Route A: leave the files | Route B: move the files | |
|---|---|---|
| WordPress's name for it | Giving WordPress its own directory | Moving directories on your existing server |
| The files | Stay in the folder | Move to the domain's main folder |
| The two addresses | Only "Site Address (URL)" changes | Both change |
| Dashboard and login | Stay under the folder, at example.com/wordpress/wp-admin/ | Move to example.com/wp-admin/ |
| Images and other uploads | Keep their addresses under the folder | Get new addresses |
| To undo | Change one setting back and remove two files | Restore the backup |
Choose route A when the folder is only untidy and all you want is the site at the root. Choose route B when the folder has to go: its name must not show anywhere, not even in the address of an image, or you are settling the site's layout for good.
Look under Settings, then General before you decide. If "Site Address (URL)" already shows the root and "WordPress Address (URL)" shows the folder, route A is already in place.
Before you start
- Back up the files and the database, both. How to schedule automatic WordPress backups covers what a full backup holds. Route B changes the database in a way that only a backup undoes.
- Try it on a staging copy first, where you can. How to set up a WordPress staging site has three ways to make one.
- Look at what is in the root now. If it holds an
index.php, anindex.htmlor a.htaccess, download copies. Both routes put files of those names there. If another site runs from the root, this move would replace it. - Write down the two addresses exactly as Settings, then General shows them now.
Route A: leave the files where they are
These are the steps in WordPress's documentation, for a site that is already in a folder. The folder in the examples is wordpress. Use your own.
Step 1: Change "Site Address (URL)" only
Go to Settings, then General. Set "Site Address (URL)" to the root,
https://example.com, with no slash at the end. Leave "WordPress Address (URL)" ashttps://example.com/wordpress. Press Save Changes. The site does not load at the root yet. WordPress's instructions say not to worry about errors at this point and to carry on.Step 2: Copy index.php and .htaccess to the root
Copy both files from the folder to the root. Do not move them.
.htaccessis a hidden file, so set your host's file manager or your SFTP program to show hidden files. A site that uses plain permalinks may have no.htaccess. Then copyindex.phpalone.Step 3: Change one line in the root's index.php
Open the copy in the root, not the original. WordPress's instructions are to change the line that says
require dirname( __FILE__ ) . '/wp-blog-header.php';to this, with your folder's name:A current copy of the file writes that line as
require __DIR__ . '/wp-blog-header.php';. The change is the same: put the folder's name in front of/wp-blog-header.phpand leave the rest of the line alone.index.phprequire dirname( __FILE__ ) . '/wordpress/wp-blog-header.php';Step 4: Log in under the folder
The dashboard has not moved. It is still at
https://example.com/wordpress/wp-admin/.Step 5: Save the permalinks
Go to Settings, then Permalinks and press Save Changes. WordPress writes the rules for the new place into the root's
.htaccess. "Permalink structure updated." means it did. "You should update your .htaccess now." means it could not write the file. Copy the rules it shows near the bottom of the screen into the root's.htaccessyourself.
Open https://example.com in a private window. The home page and a post should load at the root, with their images.
nginx has no .htaccess, and WordPress cannot change its configuration. On nginx, the line in the site's server block that hands addresses to WordPress has to name the index.php in the root, as in WordPress's own example: try_files $uri $uri/ /index.php?$args;. On managed hosting, ask the host.
What still lives under the folder. The dashboard, the login page, every uploaded file, and the theme and plugin files. So do not run a search and replace of the folder's address on this route. It would rewrite the addresses of images that have not moved. Links you typed by hand, such as custom links in a menu, still name the folder. Correct those one at a time.
To undo it, set "Site Address (URL)" back to the folder's address, remove the two files from the root, and put back anything that was there before.
Route B: move the files to the root
The order matters. WordPress's instructions are to set the addresses before the files are moved, and not to open the site in between.
Step 1: Set both addresses to the root
Go to Settings, then General. Set "WordPress Address (URL)" and "Site Address (URL)" to
https://example.com. Press Save Changes. The site stops loading until the files have moved. With WP-CLI, runwp option update home 'https://example.com'andwp option update siteurl 'https://example.com'from the folder. How to change your WordPress URL has the other ways to set the two addresses.Step 2: Move everything in the folder to the root
Use your host's file manager or SFTP. Move every file and folder,
wp-config.phpand the hidden.htaccessamong them. If route A was in place, let the originals replace the two copies in the root, and first take out of the folder's.htaccessany redirect lines you added there. Delete the empty folder afterwards.wp-config.phpneeds no change. WordPress's documentation says it does only when the database has moved, been renamed or been given a different user.Step 3: Log in at the root
Go to
https://example.com/wp-admin/. WordPress's instructions add that you may needhttps://example.com/wp-login.php.Step 4: Save the permalinks
Go to Settings, then Permalinks and press Save Changes, so that
.htaccessis rewritten for the new place. The two messages are the same as on route A. WP-CLI'swp rewrite flush --hardrewrites.htaccessonly when WP-CLI's own configuration listsmod_rewrite, so the screen is the surer way. On nginx, the rules in the server block that named the folder have to name the root.Step 5: Replace the folder's address in the database
Images, links between your pages and many theme and plugin settings are stored with the old address in full. A plain find and replace breaks some of them. The section "Replace the old address stored in your content" in the guide to changing your WordPress URL explains why and has the full steps, with a plugin for sites without WP-CLI. For this move, the search is the folder's address and the replacement is the root. Start with a dry run, which saves nothing:
Run it again without
--dry-runto save the changes. The closing slash on both addresses keeps the search to addresses inside the folder, soexample.com/wordpress-tips/is left alone.bashwp search-replace 'https://example.com/wordpress/' 'https://example.com/' --skip-columns=guid --report-changed-only --dry-runStep 6: Redirect the old folder and empty every cache
The next section has the rule. Then clear your caching plugin, your host's cache and any CDN.
Redirect the old addresses
Every old link and every search result still points into the folder. Google counts a change of path on the same domain as a site move. Its guidance is a permanent, server-side redirect from each old address to its own new one, never from all of them to the home page.
After route B: the whole folder
.htaccess
RewriteEngine On
RewriteRule ^wordpress(/.*)?$ https://example.com$1 [R=301,L]On Apache, put the two lines in the root's .htaccess, above the line # BEGIN WordPress. On nginx, the line goes inside the site's server block, and nginx has to be reloaded. /wordpress/hello-world/ then answers with a 301 to /hello-world/.
The rule catches every address that begins with the folder's name. If the site is to have a page at that same address, such as a page of posts at /blog/ after a move out of /blog/, a rule for the whole folder cannot be used. Redirect the old addresses one by one. The redirect map builder turns a list of old and new addresses into 301 redirect rules for Apache or nginx, with chains, loops and duplicates pointed out. Use it too when addresses change in more than their first part.
After route A: only what is not a real file
On route A the folder is still in use, so a rule for the whole folder would cut off the dashboard and the images. WordPress's instructions say nothing about old links to posts under the folder. Check one first, as described below. If it does not arrive, add a rule that redirects a request only when it matches no real file or folder.
wordpress/.htaccess
RewriteEngine On
RewriteRule ^$ https://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]On Apache these lines go in the .htaccess inside the folder, not the one in the root, above # BEGIN WordPress. In a folder's own .htaccess, Apache matches the pattern against the part of the address after the folder's name. On nginx, the three blocks take the place of any location /wordpress/ block the server block already has.
To undo either rule, remove the lines you added.
Check each one
Put an old post's address, an old image's address and the folder itself into the redirect checker. It shows what an address answers and every redirect on the way: each hop, its status code and where it sends you next. Each should reach its new address in one hop, with a 301. The one exception is an image after route A, which answers 200 where it is. How to set up redirects in WordPress covers writing rules, and what to do about chains and loops.
What to check afterwards
- Pages, logged out. In a private window, open the home page, a post, a page and a category. If the home page loads and everything else answers 404, save the permalinks again. How to fix 404 errors on posts and pages that exist has the rest.
- Images. Open a few older posts. After route B, a missing image means its old address is still stored. Repeat the dry run for the other forms the address has had, such as
http://orwww. - The dashboard. Log in, open a post in the editor and save it.
- Forms. Send a message through each form and see that it arrives. On a store, place an order.
- Menus. WordPress's instructions warn that menu links may still hold the old folder. Open each menu and correct them.
- The sitemap. WordPress's own is at
/wp-sitemap.xml, unless an SEO plugin provides another. Its addresses should not name the folder. Submit it in Google Search Console. Google says its Change of Address tool is not needed for a move of paths on the same domain.
If the dashboard will not load
A mistyped address, or files moved before the addresses were set, leaves the dashboard unreachable. Nothing is lost. Two constants in wp-config.php overrule what is in the database: WP_HOME for the Site Address and WP_SITEURL for the WordPress Address. Add them above the line that says "That's all, stop editing!", using your host's file manager or SFTP. These are the values for route A:
define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com/wordpress' );For route B, both are https://example.com. To go back to how things were, with the files still in the folder, both are https://example.com/wordpress.
The constants do not change the database, and while they are there the two fields under Settings cannot be edited. Once you are in, put the right values in the database and remove the lines. How to change your WordPress URL has each way of doing that. If the login page keeps coming back, work through the login page that keeps refreshing.
WordPress also documents a RELOCATE constant, which sets the WordPress Address to wherever you open wp-login.php and corrects nothing else. Its documentation calls leaving that line in place insecure. The two constants above do the job without that risk.
Common questions
Will the move hurt my search rankings?
Expect some movement. Google says rankings can fluctuate while it reads the site again, and that for a medium-sized site it can take a few weeks or more for the new addresses to show in place of the old. Its guidance is to keep the redirects for at least a year, which gives it time to pass what it knows about the old addresses to the new ones.
Can I do route A now and route B later?
Yes. Route B's steps are the same from there: set both addresses to the root, then move the files. The images change address at that point, so the search and replace and the redirect for the whole folder are needed then.
I am changing hosts as well. Which comes first?
Do one, check it, then do the other, so that a fault points at one change. A move between hosts leaves the addresses alone and is a different job. Our WordPress migration service moves one site from one host to another and tests the copy on the new host before the DNS is changed.
- ResourceWebsite migration checklist: before, during and after the move
- ResourceWebsite redesign SEO checklist: what to record, map and check on a rebuilt site
- Cost guideWordPress migration cost: which job you are being quoted for, and what moves the price
- GuideHow to move from WordPress.com to self-hosted WordPress
- GuideHow to move WordPress to a new domain and keep your links and search traffic
- GuideHow to switch from Elementor to the WordPress block editor (Gutenberg)

