{"id":153927,"date":"2016-04-13T11:00:14","date_gmt":"2016-04-13T15:00:14","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=153927"},"modified":"2022-03-02T04:05:51","modified_gmt":"2022-03-02T04:05:51","slug":"install-wordpress-subdirectory","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/install-wordpress-subdirectory\/","title":{"rendered":"When and How to Install WordPress in a Subdirectory"},"content":{"rendered":"<p>When you manage multiple WordPress sites on the same web server, keeping your installs organized is a must, especially if you look after client sites. Or maybe you just don\u2019t like installing WordPress at the root of your site. Whatever the case may be, setting up WordPress in a subdirectory provides a tidy solution.<\/p>\n<p>In this post, we\u2019re going to explore two scenarios: When you install WordPress for the first time and run the setup in a subdirectory, and when you need to move an existing installation from the root directory of your site to a sub-folder. For both scenarios, WordPress allows you to display your site as if it was installed in the domain root.<\/p>\n<p>Note: The following examples have been tested on an Apache web server, <a href=\"https:\/\/wordpress.org\/about\/requirements\/\" target=\"_blank\">as recommended at WordPress.org<\/a>. Check if <code>mod_rewrite<\/code> module is enabled and have a read over\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/understanding-file-permissions\/\" target=\"_blank\">Understanding File Permissions<\/a> if you need a refresher about on this topic.<\/p>\n<p>Continue reading, or jump ahead using these links:<\/p>\n<ul>\n<li><a href=\"#installing-wordpress-in-a-subdirectory\">Installing WordPress in a Subdirectory<\/a><\/li>\n<li><a href=\"#moving-wordpress-from-the-domain-root-to-a-subfolder\">Moving WordPress from the Domain Root to a Subfolder<\/a><\/li>\n<\/ul>\n<h2 id=\"installing-wordpress-in-a-subdirectory\">Installing WordPress in a Subdirectory<\/h2>\n<p>Download the most recent copy of WordPress and install it in a subfolder of your choice on your web server. If you need some help installing WordPress, check out our post <a href=\"https:\/\/wpmudev.com\/blog\/guide-installing-wordpress\/\" target=\"_blank\">A Guide to the Best Ways to Install WordPress<\/a>. For this example,\u00a0I installed WordPress in the\u00a0<em>\/wp_install<\/em> folder on my web server.<\/p>\n<p>Once you&#8217;re done setting up WordPress, go to\u00a0<strong><a href=\"https:\/\/codex.wordpress.org\/Settings_Permalinks_Screen\" target=\"_blank\">Settings &gt; Permalinks<\/a><\/strong> screen and set your favorite Pretty Permalink structure. This will generate a <em>.htaccess<\/em> file, a directory-level configuration file used for site access issues, such as URL redirection and URL shortening. Sometimes WordPress doesn&#8217;t automatically generate or update your<em> .htaccess<\/em> file. In this case, you should check if WordPress has write access. You can read up on how to change file permissions in the <a href=\"https:\/\/codex.wordpress.org\/Changing_File_Permissions#.htaccess_permissions\" target=\"_blank\">WordPress Codex<\/a>. The 644 settings is recommended.Now go to<\/p>\n<p>Now, go to <strong>Settings &gt; General<\/strong> in the backend of your WordPress sites and look for the\u00a0<strong>WordPress Address (URL)<\/strong> and<strong> Site Address (URL)\u00a0<\/strong>fields. WordPress Address is the address of the folder where you&#8217;ve installed WordPress, and the Site Address is your website&#8217;s URL.<\/p>\n<p>Both addresses should always include <em>http:\/\/<\/em> and never the final slash. By default, these fields have the same value.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/04\/wp_options_example.png\" alt=\"wp_options table\" width=\"659\" height=\"121\" \/><figcaption class=\"wp-caption-text\">WordPress Address and Site Address values are stored into wp_options database table<\/figcaption><\/figure>\n<p>Next, remove the <em>\/wp_install<\/em> part from the Site Address (URL), so that:<\/p>\n<ul>\n<li>WordPress Address (URL) is\u00a0<em>http:\/\/example.com\/wp_install<\/em><\/li>\n<li>Site Address (URL) is\u00a0<em>http:\/\/example.com<\/em><\/li>\n<\/ul>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/04\/general_settings.png\" alt=\"You can set a custom value for the Site Address field in the general settings in the backend of your WordPress install.\" width=\"625\" height=\"341\" \/><figcaption class=\"wp-caption-text\">We can set a custom value for Site Address field in General settings screen<\/figcaption><\/figure>\n<p>Save the settings you just edited, but don&#8217;t\u00a0refresh your site just yet.<\/p>\n<p>Now, using your preferred FTP client (mine is FileZilla), login to your site and copy the <em>index.php<\/em> and <em>.htaccess<\/em> files from your WordPress folder (<em>\/wp_install<\/em> in this example) and paste them into the domain root (these files should always be together in the directory you set in the Site Address field \u2013 that&#8217;s the root in our example). <strong>Be careful to copy and paste these files, not just move them from your WordPress directory.<\/strong><\/p>\n<p>If the\u00a0<em>.htaccess<\/em> file is not visible, this might be because it doesn&#8217;t actually exist (i.e. pretty permalinks aren&#8217;t enabled) or because your FTP client doesn&#8217;t show hidden files. This may also occur if you&#8217;re working from a localhost environment and your computer recognizes <em>.htaccess<\/em> as a system file and hides it. Whatever the reason, you can force your FTP client to show hidden files, as illustrated in the image below:<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/04\/show_hidden_files.png\" alt=\"In FileZilla, you can force hidden files from to display from the Server menu.\" width=\"575\" height=\"180\" \/><figcaption class=\"wp-caption-text\">In FileZilla, you can force hidden files from to display from the Server menu.<\/figcaption><\/figure>\n<p>Open the\u00a0<em>index.php<\/em>\u00a0in the root directory of your domain in a plain text editor and replace the following line of code:<\/p>\n<div class=\"gist\" data-gist=\"08b053ccbf1f99ec726d68f72494a6ff\" data-gist-file=\"gistfile1.txt\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/08b053ccbf1f99ec726d68f72494a6ff.js?file=gistfile1.txt\">Loading gist 08b053ccbf1f99ec726d68f72494a6ff<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>with this line of code:<\/p>\n<div class=\"gist\" data-gist=\"ccfb87e9ef9a4d77111949f17c4e0af2\" data-gist-file=\"gistfile1.txt\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/ccfb87e9ef9a4d77111949f17c4e0af2.js?file=gistfile1.txt\">Loading gist ccfb87e9ef9a4d77111949f17c4e0af2<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>That&#8217;s all you need to do. Now your website will be served from the root directory of your web server.<\/p>\n<h2 id=\"moving-wordpress-from-the-domain-root-to-a-subfolder\">Moving WordPress from the Domain Root to a Subfolder<\/h2>\n<p>If you decide to move an existing WordPress site from the domain root to a subfolder, a complete backup of your site is absolutely necessary because the following steps involve\u00a0updating\u00a0your database tables. If something goes wrong while moving files and updating tables, it&#8217;s better to be safe than sorry.<\/p>\n<p>If you need some tips on the best ways to backup your site, check out\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/backup-with-snapshot\/\" target=\"_blank\">How to Backup Your WordPress Website (and Multisite) Using Snapshot<\/a>\u00a0and\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/multisite-backup-solutions\/\" target=\"_blank\">4 Top WordPress Multisite Backup Solutions Tested and Reviewed<\/a>.<\/p>\n<p>Next, using your favorite FTP client,\u00a0create a new folder on your web server, i.e. <em>\/wp_install<\/em> or whatever you like.<\/p>\n<p>Then you need to login to the backend of your site and go to <strong>Settings &gt; General<\/strong> and\u00a0change the URL values. Set <strong>WordPress Address<\/strong> to the folder URL and <strong>Site Address<\/strong> to the main site URL, i.e. the same values as the example earlier in this post.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/04\/wp_option_subfolder.png\" alt=\"The wp_option table stores the addresses set on the Settings &gt; General page.\" width=\"659\" height=\"120\" \/><figcaption class=\"wp-caption-text\">The wp_option table stores the addresses set on the Settings &gt; General page.<\/figcaption><\/figure>\n<p>Next, going back to your FTP client, move the whole WordPress install from the root directory to the<em> \/wp_install<\/em> folder you created, keeping a copy of the\u00a0<em>index.php<\/em> and <em>.htaccess<\/em> files in the root. Then open the root <em>index.php<\/em> file and replace\u00a0the following line:<\/p>\n<div class=\"gist\" data-gist=\"65c2f277c84e729c79f280dfb47016f7\" data-gist-file=\"require-statement\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/65c2f277c84e729c79f280dfb47016f7.js?file=require-statement\">Loading gist 65c2f277c84e729c79f280dfb47016f7<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>with this line of code:<\/p>\n<div class=\"gist\" data-gist=\"0d3651545b4a8387353ce279a583e83f\" data-gist-file=\"require-statement\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/0d3651545b4a8387353ce279a583e83f.js?file=require-statement\">Loading gist 0d3651545b4a8387353ce279a583e83f<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>With these changes in place, WordPress will look for <em>wp-blog-header.php<\/em> in its new location.<\/p>\n<p>Now, go to <em>http:\/\/example.com\/wp_install\/wp-admin\/<\/em>\u00a0(replacing &#8220;<em>example.com&#8221;<\/em> with your own domain, of course) and log in to the backend of your site. Go to\u00a0<strong>Settings &gt; Permalinks<\/strong>\u00a0and set up your preferred\u00a0pretty permalinks structure.\u00a0Note that you may be required to save the permalink settings even if you do not make any changes to the permalink structure.<\/p>\n<p>Lastly, it&#8217;s time to review the internal links in your navigation menus and post content. You could easily change the menu links from the Appearance &gt; Menus page but it would be a little tricky changing links in existing posts. You could\u00a0replace each occurrence of the old URL by running a query from your database or, better still, you could use a plugin to\u00a0execute the same query.<\/p>\n<p>Using the\u00a0<a href=\"https:\/\/wordpress.org\/plugins\/better-search-replace\/\" target=\"_blank\">Better Search Replace<\/a> plugin, you can comfortably search and replace strings in WordPress. Just go to\u00a0<strong>Tools &gt; Better Search Replace\u00a0<\/strong>in the backend of your site once you&#8217;re installed and activated the plugin.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/04\/better-search-replace.png\" alt=\"You can replace the old Site Address occurrences in wp_posts table with a plugin like Better Search Replace.\" width=\"735\" height=\"554\" \/><figcaption class=\"wp-caption-text\">You can replace the old Site Address occurrences in wp_posts table with a plugin like Better Search Replace.<\/figcaption><\/figure>\n<p>Our task is accomplished! Visitors to your site won&#8217;t see any difference and will access\u00a0your\u00a0website from the domain root.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you manage multiple WordPress sites on the same web server, keeping your installs organized is a must, especially if you look after client sites. Or maybe you just don\u2019t like installing WordPress in the root of your site. Whatever the case may be, setting up WordPress in a subdirectory provides a tidy solution.<\/p>\n","protected":false},"author":387958,"featured_media":154045,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[10020,10460],"tutorials_categories":[],"class_list":["post-153927","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-subdirectory","tag-subfolder"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/153927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/387958"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=153927"}],"version-history":[{"count":25,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/153927\/revisions"}],"predecessor-version":[{"id":209433,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/153927\/revisions\/209433"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/154045"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=153927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=153927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=153927"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=153927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}