Installing WordPress in a Subdirectory

There are two reasons I like to install WordPress in its own folder, rather than the  root. First, I like to keep my root folder uncluttered. I don’t like a bunch of WordPress files that I’ll never be messing with getting in my way.  That’s just a matter of personal preference though. The second reason is for security. To prevent automated site scans from easily finding my WordPress files, I simply place them in a separate directory. So… here’s how you do it. It’s actually quite simple.

1. Install WordPress in a subdirectory

Just follow the instructions I gave in my post Installing WordPress Manually. Make sure you create a new directory and upload the WordPress installation files there (that’s step three in the post I just mentioned).

2. Edit the index.php file

Once WordPress is installed, download the index.php from the directory where you uploaded WordPress. You’ll need to open it up and make one small change. Find this piece of code:

/** Loads the WordPress Environment and Template */<br />
require('./wp-blog-header.php');

All you need to do is tell it how to find wp-blog-header.php in its new subdirectory. For example, here’s what the code would look like if I installed WordPress in a directory called example.

/** Loads the WordPress Environment and Template */<br />
require('./example/wp-blog-header.php');

Notice how I added /example just before /wp-blog-header.php. So just add the name of your directory in place of example and that’s all the code editing you have to do.

3. Upload the new index.php file

Now that you’ve made the necessary change to your index.php file, it’s time to upload it to your root directory. Feel free to leave the old index.php file in your WordPress directory, as this will prevent users from looking at the contents of WordPress directory.

4. Tell WordPress what you did

Finally, you need to let WordPress know about the change. First, log into WordPress. The login page will still be at the old URL (for example https://alexmansfield.com/example/wp-login.php not https://alexmansfield.com/wp-login.php). Go to Settings>General and change the Blog Address field (for example, from https://alexmansfield.com/example/ to https://alexmansfield.com/). Save your changes and that should do it!

If you have any questions, please ask them in the comments section below.

Posted in

25 thoughts on “Installing WordPress in a Subdirectory”

  1. I installed wordpress in a subdirectory and this is about third article I read stating the same steps. But I still get the directory index displaying after I log into the admin panel. In addition, I go to my directory I installed wordpress and I get the directory index as well. No page is generated. Any help would greatly be appreciated. Thank you so much.

  2. There could be a few different things causing this. When you see the directory index, is there an index.php file listed? Also, do you know what operating system your server is running?

  3. Just one small problem.

    The name of your subdirectory appears in the page’s source code. Plus, if you link to an image, the img src will also include the subdirectory name.

    I thought this was to “hide” the location of the wordpress files? Any ideas?

  4. That’s true, SFGolfer. The purpose of using a subdirectory isn’t really to hide WordPress from prying eyes. Rather, it moves the WordPress files away from the default location so that scripts that look for them in the usual spot don’t see them. The original posts wasn’t clear about that, so thanks for pointing it out. I’ve edited the beginning of the post to reflect that issue.

  5. Like I mentioned to Carlos, there could be a number of different causes. It could be a problem with the file permissions not allowing WordPress to run the scripts necessary to generate the page. If you’d like, you can contact me through my contact form with a link to your site and I can try to help you figure it out.

  6. Wow forgot about this problem running into it again. Guess I was never able to figure out. To answer. I copied the .htaccess file set the directory index to default to index.php and modified the index.php file to load the header php file from the subdirectory and still getting the same rendering. I went to the admin panel and modified the blog url but when I did it jacked up my admin panel so I had to go via PHP admin and reset the value using SQL. If by any chance anyone has an idea, it would be great to know. I’ve looked and looked. To no avail. Thanks a bunch!

  7. Hi Carlos, I’m not sure what to tell you, since I’m not familiar with WAMP. I haven’t needed to mess with .htaccess when installing WordPress to a subdirectory. Let us know if you get anything figured out.

  8. Hi Alex –

    I was running along just fine when I installed a plug in that caused deprecated errors to appear.

    I decided to add

    define(‘WP_DEBUG’, false);

    to my wp-config.php file. I uploaded it and now the site runs, but all attempts to reach wp-admin get redircted to:

    http://fastfingersguitarlessons.com/wp-login.php?redirect_to=http%3A%2F%2Ffastfingersguitarlessons.com%2Fwordpress%2Fwp-admin%2F&reauth=1

    it should redirect to fastfingersguitarlessons.com/wordpress/wp-login.php

    I renamed the plugin directory to plugin.hold to remove them. No change.

    any ideas? I posted this to a forum here 2 weeks ago and got zero replies.

    Spencer

    1. It sounds like the plugin must have made some modifications to the database or the .htaccess file. I would suggest contacting the author of the plugin you mentioned to see if they have any idea what changes might have been made by their plugin.

  9. First of all, thank you for a very informative article – I hope I can use it for what I think I can use it for. If not it still made me smarter

    I am interested in making an English version of my site and has been looking over quite a few plugins, but some are only working for post translations, and some are not compatible with the events calendar I am using.

    So, I looked around on the WWW to search for other solutions and I came across the suggestions to create a sub-folder on my FTP and copy all my wordpress content into that folder and get to work translating the whole site.

    However, I am not at all a wordpress expert, so I don’t know if this would work – so I am hoping for your advice.

    Will I be able to log into 2 different wordpress panels – one for the .com site and one for the .com/en to edit content, menus etc. or what will you recommend me to do?

    I really hope that you can give me some advice on this

    Thank you so much in advance.

    Kind regards,
    Helle

    PS. Had to post again as I entered wrong e-mail address.

    1. What you are suggesting would be possible, but it wouldn’t be accomplished with the methods explained in this article. This article just explains how to move all the WordPress files outside your root directory. It doesn’t result in 2 copies of the same site. To do that, you would need to install WordPress a second time, using a second database, etc. Hope that helps!

  10. I followed your instructions and as soon as I changed the site address from amirkhan.co/wordpress to amirkhan.co I got a 404 page not found error. When I log back in to the admin page by going to amirkhan.co/wordpress/wp-login.php as soon as i hit the login page i get a 404 page not found again. The login is trying to go to amirkhan.co/wp-admin instead of amirkhan.co/wordpress/wp-admin. Any idea how i resolve this?

  11. I have the same issue. I followed your steps exactly except putting my site in a “blog” folder (and the site works!! woot!) but I am not able to get to my admin anymore. Neither http://example.com/wp-admin nor http://example.com/blog/wp-admin works.

    Only URL that works is http://example.com/wp-login.php. When I go there I get a really funky unstyled page, like WordPress is broken or something. And then when I login the Admin bar appears at the top of the site, but none of the admin links will work – they all go to 404 not found pages.

    Hoping you might have an answer!

    1. I think there could be a few different things that could cause such an issue. If you want to use the contact form on this site to send me a link to your website, I would be happy to take a look and see if I can figure out what went wrong.

  12. The WordPress.org website (http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory) says to COPY, not move, the index.php and .htaccess files from the subdirectory into the root directory. I’ve read through a number of other sites, and while most of them also recommend copying, not moving, some instruct the user to move, not copy.

    Your instructions recommend deleting the old index.php file in the subdirectory after you upload the edited version of index.php to the root directory. In other words, if I understand correctly, move, not copy. I’m just wondering if there’s a particular reason for deleting the file from the subdirectory. Will it cause problems?

    1. Thanks for mentioning that! I just did a little testing and copying is the correct method. I’m not sure if there were issues with copying in the past when I wrote this tutorial or not, but I’ve updated the post to specify that the original index.php file should be left intact. Thanks again!

  13. thanks a lot..
    i followed your procedure …. and it is successfully install.
    i install wordpress in subdirectory … and it is working perfect.

Leave a Reply to alexmansfield Cancel Reply

Your email address will not be published. Required fields are marked *