Search code examples
wordpressurlsubdomainsubdirectory

Changing Wordpress Site URL from Subdomain to Sub Directory


This is a 911; I need to change a Wordpress subdomain URL that I created a GoDaddy WP installation to a subdirectory URL. Can't I just make a folder in my FTP root site folder and transfer the WP files over? No migration plugins work.

I know that I should've installed the WP on GoDaddy on the proper root directory, to begin with, but I didn't think it would be this difficult to fix.

Just so I'm clear I want example.myrooturl.com to be myrooturl.com/example. I need the most straight forward, quickest way to do this if possible


Solution

  • I have had to do the reverse of what you're requesting quite frequently. Moving a Wordpress site can be tricky as the site's canonical URL is stored in the database and a lot of the internal links are also stored with the full URL (not a relative one). In my experience this can be done in 5 minutes with a few steps but be prepared for a few minutes of downtime:

    1. Perform a export (backup) of your database and download it to your local machine (backup using the sql format) probably easiest using PHPMyAdmin
    2. Open the sql file in a good text-editor (Notepad++ is good on Windows or Text-Wrangler on Mac)
    3. Perform a "Find and Replace" in the database, find all instances of your current url "example.myrooturl.com" and replace it with the new url "myrooturl.com/example"
    4. Select all text in the file and copy to your clipboard (Ctrl+c or Cmd+c)
    5. Connect to the FTP server using an FTP program (FileZilla is best IMHO)
    6. Create a sub-folder in the site's root directory called "example" (where "example" will be the /example part of the url)
    7. Navigate to the site's root - probably a folder called "example.myrooturl.com" in the root directory
    8. Select all the files in that directory
    9. Drag those files to the newly created "/example" folder
    10. Go back to PHPMyAdmin and paste the copied sql statements from step 4 into a query window - this may take a few seconds to a minute or two for the text to appear in the textarea
    11. Execute the sql query

    If these steps were followed and no issues encountered this should be all you need to do! You should be able to navigate to the new URL and view the site.

    EDIT

    Two things weren't considered: the removal of the sub-domain which, in CPanel, was mapped to the sub-folder we wanted to use; and the fact that the site in the sub-folder would be nested within another Wordpress installation.

    The first item is straightforward, remove the sub-domain mapping to the folder and clear browser cache. The second one is more challenging and involves editing the .htaccess in the root of the public_html folder (the one for the main site). This is because when you have a wordpress site nested in another installation, Wordpress is likely not to allow traffic to go to the nested site (site.com/site) and will instead generate a 404 (if a permalink doesn't already exist for the sub-folder name). This is done by editing the htaccess as follows (taken from here)

     # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    
    # Include in the next line all folders to exclude
    RewriteCond %{REQUEST_URI}  !(folder1|folder2|folder3) [NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress 
    

    It's also worth deleting the old .htaccess from the nested site, logging into the wp-admin, visiting the Settings->Permalinks page and clicking "save" to regenerate the .htaccess in the context of the new installation.

    NB @user7357089 suggests performing 301 redirects, this is a good idea to maintain any SEO "juice" that you have as well as prevent broken links. This can be done easily with the .htaccess file - if you have experience here, let me know if you need guidance