Changing Wordpress Site URL from Subdomain to Sub Directory

↘锁芯ラ 提交于 2019-12-25 08:32:07

问题


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


回答1:


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




回答2:


Rewrite your wp-config.php with this code

define('SUBDOMAIN_INSTALL', false);

instead of define('SUBDOMAIN_INSTALL', true);

then goto:

http://www.website.com/wp-admin/network/setup.php

then:

Add the following to your .htaccess file in /var/www/vhosts/website.com/, replacing other WordPress rules:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

That's it.

Create a New Site with Subdirectory




回答3:


I had some trouble after doing all steps I found in different guides. After search and replace my newly changed en.domain.com to domain.com/en my en site didn't show anything. After som troubleshooting I found that I had to change the domain and path as-well in the wp_blogs table, I haven't seen this in any of the guides before I did the domain to subdirectory switch. So I thought I'd give a hint if you are struggling as I did ;)




回答4:


Sub-domain or sub-directory is the important part when you installed multi-site WordPress, you will find this option when you are trying to setup your Network on WordPress.If you selected sub-domain then you need to re-install WordPress because it’s a permanent change and the only solution is to re-install and configure the WordPress again. I have attached the pic where you'll find this. Thank you! enter image description here



来源:https://stackoverflow.com/questions/41477620/changing-wordpress-site-url-from-subdomain-to-sub-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!