Giving WordPress its own directory while leaving your blog in the root directory
SandboxBlogger, WordPress April 4th, 2007Typically, when I install WordPress, I usually install it in the root directory of the website. That is because I prefer not to host too many things under one domain name and I really don’t like overly long URL. Simply put, I like my blogs accessible via URL like http://example.com/ rather than http://example.com/my-wonderful-blog.
Installing WordPress in the root directory of the website is usually not a problem but really soon, I would start to optimize my blog for search engine and usually means, placing files like robots.txt, and sitemap.xml. Add a .htaccess file to support the customize the URL. Then there are files for ownership verification required by services like Yahoo! Site Explorer to help clutter up the root directory even more. That is all fine until one day a new version of WordPress is released and it is time to upgrade (just like TODAY!)
Every time I need to upgrade, I find myself doing a lot of cross referencing to make sure I’m not deleting the wrong files. This time around, when I’m installing WordPress for SandboxBlogger.com, I’ve came across this instruction on the WordPress.org. What a great idea! Basically, this simple guide showed me how to install WordPress in a subdirectory off the root directory of the website yet the URL will still remain as http://www.example.com/. Also, all other files I place in the root directory of the website is still accessible via URL like http://www.example.com/sitemap.xml.
For those interested, here’s how:
- Unzip wordpress package in the root of your website. Assuming the root of your website is ~/public_html/, you will now have ~/public_html/wordpress (wordpress unpacks into a subdirectory named wordpress). Move the wordpress directory to another directory name like myblog. I assume you will want to but if you don’t, remember to substitute myblog to the actual directory name you want to use.
- Proceed to install WordPress as the installation guide said and use this URL: http://example.com/myblog/wp-admin/install.php. Your blog will now have the following URL http://example.com/myblog/.
- Login and go to the Options panel.
- In the box for “WordPress address (URL):” you should see http://example.com/myblog/ leave that alone.
- In the box for “Blog address (URL):” change the address to the root directory’s URL, like http://example.com/. Click on the picture below to see where in the WordPress Dashboard to make the changes.
- From the file system, copy index.php from ~/public_html/myblog/index.php to ~/public_html/index.php.
- Open the ~/public_html/index.php file in your text editor and change the following line:
require(‘./wp-blog-header.php’);
to the following:
require(‘./myblog/wp-blog-header.php’); - If you need to customize your permalink, remember to create your .htaccess file in ~/public_html/.htaccess
Remember to also place all other files that do not belong to WordPress in ~/public_html/. Example files like robots.txt and sitemap.xml.
October 6th, 2007 at 5:09 am
I’m currently applying this tutorial to my blog. Two problems I’m having are what are all the necessary files that I need to move? and the .htaccess can be created like any file? Do I create a blank file named .htaccess? I don’t see a notify me of comment button. Can you email me once your reply please?
October 8th, 2007 at 5:15 am
Hi Alex,
There is really no files you need to move unless you want to install WordPress in a directory other then …/public_html/wordpress. To minimize confusion, replace all “myblog” with “wordpress” in the tutorial. One thing to remember, in the tutorial, public_html is the directory where it is the root of your site. Meaning that if you place a file call “pictures.html” in the public_html directory and your site is example.com, then you can access the file with this URL: http://example.com/pictures.html
The other two files that are required to be in the root of your site are the index.php and .htaccess. But instead of moving the index.php, you will want to copy that file from public_html/wordpress/index.php to public_html/index.php.
As for the .htaccess, when you customize the url of your blog in the “Options | Pemalink” page, you are given the content of the .htaccess file after you have save the options. Just create the .htaccess with what WordPress has provided. Unless if you want to let WordPress handle that .htaccess file, just create an empty file and set the permission to allow access to everyone. I personally would just create that file with what WordPress provide.
Hope that helps.
BTW, if you want to keep track of the comment on this blog or want to get notified of reply to your comment, you can always subscribe to the comments RSS feeds. The link is at the bottom of the footer on this page.
October 26th, 2007 at 10:21 am
What about an existing site that was already installed and running on a sub directory (myblog in you example) …
Can I now go back and change the options as above to make the blog appear as if it was on the root?
Will all my old posts be ok?
October 26th, 2007 at 5:49 pm
Hi Doug,
For existing users, it makes no difference and the move is relatively easy. Just copy or move those files and make the configuration changes as in the guide.
October 28th, 2007 at 11:01 pm
hi, i tried the tutorial for giving wordpress a new directory. this is what i did and what happened to me:
1. i have my blog at http://example.com/wp
2. i went to options, change the blog url to example.com
3. i have only my .htaccess at my root directory so what i did was move the index.php file from public_html/wp to public_html.
what happened is that i cannot access my example.com and example.com/wp anymore; i get a 500 internal server error.
4. i tried moving back the index.php to public_html/wp but still same error.
what should i do?
October 28th, 2007 at 11:07 pm
oh, nevermind. i found out what happened. this is what did:
1. after i returned the index.php to public_html/wp, i then “copied” (not “move” anymore like i did the first time) index.php to public_html.
2. i edit public_html/index.php and changed the require(’./wp-blog-header.php’); to require(’./wp/wp-blog-header.php’);
3. i still got the same 500 internal error, so i tried checkin the contents of my public_html/.htaccess and i saw that there was only two lines added ” # BEGIN WordPress
# END WordPress”
4. so what i did was remove that two lines and saved my .htaccess and when i tried typing my example.com url, my blog already showed! yey!
October 29th, 2007 at 2:42 am
kuting,
I’m glad you have solve your problem but it seem strange the two lines in the .htaccess was causing the problem. Just curious, is your hosting service on a Windows server?
February 20th, 2008 at 1:46 pm
If I currently have the WordPress URL as http://www.michaelaulia.com/blogs and want to change so that it becomes:
http://www.mihcaelaulia.com to access WordPress, will the old link still work (../blogs/my-post.html) after I change the option?
February 20th, 2008 at 3:01 pm
Michael,
Interesting question. I’ve not tried it myself but I suppose it should work (Err… I mean I don’t remember). I’ll give it a try later and update the post. If you decide to try the move before I have a chance to try it out, please make sure you have your blog backed up before attempting the move.
February 21st, 2008 at 12:04 am
Michael,
OK, I’ve had a quick test and unfortunately no, old link will not work without some .htaccess magic. If you need help with that, just let me know. I’ll probably write a post about it over the weekend.
EDIT: OK, apparently it works. I don’t know why it did not work the first time I’ve tried it but it works. I’ve tested with WordPress 2.3.3 and by changing the blog location, WordPress will redirect your old link to the new link. The only exception is WordPress will not handle the home page. e.g http://example.com/blog will give you a 404 error after the move from http://example/blog to http://example.com.
February 21st, 2008 at 7:07 am
Ah I see! Really appreciate you for trying it out just for me!
I’m still in the dillema whether to keep my blog as /blogs (already have good traffics) or move the URL to my root domain *ugh*
February 22nd, 2008 at 4:51 am
Michael,
Although I would love to seize the opportunity to do some PR, I’ll have to be honest. I’m such that I must find answers to burning question in my mind. However, you are the one who have put it there, so I’d have to try it out so that I can answer it for you too. 😀
As for the question of whether to move your blog or not, I have the following to say. The usual reason why one would want to move a WordPress installation to a subdirectory is for reason of easier upgrade procedure. On the other hand, to configure WordPress to serve the blog from the root is mostly a matter of preference. Although some may argue that moving the blog to the root of the site is better for SEO but I’ve seen the same argument to keep the blog in a subdirectory call /blog/. I’ve also seen very successful blogs that are configured to live in root as well as in a subdirectory /blog/. So, to go with root or /blog/, it is really not a huge factor as to whether the blog is going to be successful or not.
If your blog is new and you are asking whether to move or not, I would say go ahead if that’s your preference. However, I would say think twice if your blog already have good traffic and if readers are able to find your blog’s URL by going to the main page of your blog.
I hope the above will point you in the right direction.
February 22nd, 2008 at 6:13 am
Hey, cheers very much for that, Han. I honestly don’t want to move my blogs cause it has 200 unique traffics a day (I know it’s not much yet, but it’s something)
I’ll reconsider moving it to “blog” (I don’t know why I even put ‘s’ at the back *Doh!!*) and probably setup a 301 to it (need to google a bit first)
Thanks again, you don’t how many times I’ve refreshed your page 😀
February 22nd, 2008 at 2:43 pm
Michael,
If it is up to me, I’ll say don’t change. As for moving from /blogs/ to /blog/, I’d say change only if you have a lot of 404 from people typing in …/blog on the address bar. If you don’t get a lot of that, there is little point changing. I don’t really see the difference between /blogs/ and /blog/.
So you are the one skewing my stats! Haha…
February 25th, 2008 at 8:04 am
LoL Thanks for that Han
I’ve decided not to changet anything, but put an index.html under /blog/ that forwards to my blogs 🙂
February 25th, 2008 at 3:31 pm
Micheal,
That’ll work too. 😀
March 9th, 2008 at 4:11 am
Hey, I was wondering if you could help me out with something. I managed to successfully do this process from mobilegreen.org/blog to mobilegreen.org, but for some reason when I go to http://www.mobilegreen.org, I only have a blank page! Any help would be much appreciated.
March 9th, 2008 at 4:12 pm
Hi Ko,
It looks like something you did cause an error. The question is what did you do different or what is different in your environment. By looking at your blog, there is no indication of what is the cause because there is absolutely nothing in that page. Please read through the tutorial again and point out anything you may have done differently. Also, is this is your initial installation or you have a blog going and you are trying to move the url?
The other thing to look out for is, do you have any stray files in your installation? Example, do you have index.html in your web root? If so, check if those files could cause the problem you are seeing.
April 1st, 2008 at 12:17 am
hi. great info here! thanks!
i am just in process of setting up my wordpress blog and i would like it to be what people see first when they go to http://www.kpphotography.ca
my problem is that i don’t have an .htaccess file…. at least not that i can see. and i’m not sure if i am understanding your instructions in comment #2.
“As for the .htaccess, when you customize the url of your blog in the “Options | Pemalink” page, you are given the content of the .htaccess file after you have save the options. Just create the .htaccess with what WordPress has provided. Unless if you want to let WordPress handle that .htaccess file, just create an empty file and set the permission to allow access to everyone. I personally would just create that file with what WordPress provide.
Hope that helps.”
where do you see the contents of the .htaccess file? i’m not seeing that.
April 1st, 2008 at 12:38 am
Hi kathy,
The .htaccess content can be seen when you go to “Options | Permalinks”. You need to scroll to the bottom of that page. If you don’t have that file writable by WordPress, you need to create that file in the root of your blog and paste the content you find from the WordPress options page as mention above.
April 1st, 2008 at 1:08 am
thanks han! and i’m sorry to be such a bother, but what exactly will that content look like? is it the info that is in the ‘custom structure’ box?
when i created the .htaccess in the root of my blog, i got an error page.
i’m so confused by this.
April 1st, 2008 at 1:23 am
i just figured it out! 🙂
thanks so much for your kind help! 🙂
have a GREAT day!
April 1st, 2008 at 4:04 am
kathy,
It’s not the ‘custom structure’ box. It’s the box at the bottom of the page, but you already knew that. 😀
Glad you have figured it out. You have a great day as well and happy blogging. 😀
August 9th, 2008 at 5:39 am
hello,
i installed the WP 2.6 at the directory (instead of the root) using fantastico install and changed the blog url to reflect the main url. i also changed the index.php as per instruction. as i cannot directly copy it, i uploaded the edited index.php via filezilla to the public htlml folder. i cannot see the ht access file from the directory folder, but it is in the root folder. When i view site, it is showing the hosting provider? I do not know how to figure it out as this is my first time to install WP.
Your help will be appreciated.
August 9th, 2008 at 6:37 am
Further to my question, basically how do you COPY the index.php and .htaccess files from the WordPress directory into the root directory of the site? In my case, I did not see any .htaccess files from the subdirectory, the file is already in the public_html. I am getting error messages..
August 14th, 2008 at 4:31 am
Hi vi,
What you need generate the .htaccess before setting the blog address. The easiest way to do this is to change permalink. If you have the right permission, it will be updated for you. If not, WordPress will show you what to do.
To change permalink login to the admin panel and go to the “Settings” tab. Next, click on “Permalinks” to change your permalink. You can pick either one of the pre-defined link format or “Custom Structure” and customize it how ever you like. If you don’t have the right permission on that directory (99.999% of the case), WordPress will show a text box at the bottom of the “Permalinks” page once you have save your new permalink format. In that case, you will need to copy the text in the textbox and create the file on your computer. You can now upload the .htaccess instead of copying over.
Alternatively, you can also open up the .htaccess with cpanel in your public_html directory and modify it based on WordPress suggestion above.
If in the event that you do have the right permission to create the file via WordPress, then you can continue with the rest of the guide and copy the .htaccess file to the correct location.
Hope that helps.
September 23rd, 2008 at 4:55 am
I have tried to move my wordpress access to the root directory as you outline above. However, I get the following error messages:
Warning: main(./wordpress/wp-blog-header.php) [function.main]: failed to open stream: No such file or directory in /export/home/he/herita51/public_html/index.php on line 17
Fatal error: main() [function.require]: Failed opening required ‘./wordpress/wp-blog-header.php’ (include_path=’.:/usr/local/php4cgi/lib/php’) in /export/home/he/herita51/public_html/index.php on line 17
Any ideas of what to do next would be most appreciated.
September 23rd, 2008 at 7:46 am
Please disregard my last post. I turns out to be an issue of letter case usage.
Thanks
September 30th, 2008 at 8:46 pm
I am facing the problem of duplication
http://www.mysite.com
http://www.mysite.com/blog
both are showing me the same page. I created one page called “BLOG”, published it, and then set it as Front Page by Options > Reading.
I want to show welcome message, post titles, short description on Front Page.
and normal blog when user click example.com/blog
just like this : http://alexking.org/
October 1st, 2008 at 2:00 am
mrugesh,
Sorry, the instruction on this post is not meant to address what you want to do. What you want to do needs to be done on the theme.
January 1st, 2009 at 11:17 pm
Thanks for the great guide! It should work perfectly if followed correctly… AND you’re not replacing an old MT install, like I was. I followed your instructions and the index appeared correctly but all archived pages were appearing without stylesheets. I discovered that this was because the .htaccess redirects only take effect if the destination does not exist. I was still pointing to the old file structure from my old Movable Type install. As soon as I removed (or renamed) the old MT install and directories, the new .htaccess took over and everything was kosher. Thanks again for the guide!
January 3rd, 2009 at 12:54 am
Hi, have a question. I’ve used this method for my site… wordpress installation is located in /blog. I’ve set it up via permalinks so that all my posts are under /blog/post-slug, and that all my pages are under /page-slug (root directory). root url pulls up /index.php, and /blog pulls up /blog/index.php (one is “home” page, one is recent posts).
This worked well for me in WP 2.6… but I just upgraded to 2.7 and now am experiencing an error when I try to load the /blog page (I left a index.php file in the blog directory so that it could load recent posts).
Any suggestion for fixing this issue?
January 3rd, 2009 at 2:14 am
(regarding my earlier comment. I found a temp workaround — moving my wordpress install to a different directory than blog. But I’d really like to put things back if possible!)
February 26th, 2009 at 2:01 pm
I just installed wp on my own domain. I followed your instructions and it worked.. however whenever I click on anything on the website like comments.. it shows it downloading to my desktop. The same problem happens in my admin dashboard. Anything I click on downloads the file to my desktop. Can someone tell me like a 5 year old what to do?
February 26th, 2009 at 2:07 pm
I don’t know what happened but its not doing it anymore. Love this blog!
Thanks.
March 16th, 2009 at 6:15 pm
I just installed WP in public_html. When i view mywebsite.com, i get the following error:
Warning: Cannot modify header information – headers already sent by (output started at /home/manish17/public_html/wp-config.php:1) in /home/manish17/public_html/wp-includes/functions.php on line 2575
Error establishing a database connection
but when i view it on the mywebsite.com/public_html, i see the website. Please let me know how to get the site on mywebsite.com only. Thanks much!
June 1st, 2009 at 10:23 pm
Dear friend I followed your tutorial again and again without success, my colleague has also tried on localhost and managed to make it, but on the server the mission seems impossible.
Would you have any idea for us or would you please like to have a look at it, if you don’t mind?
June 1st, 2009 at 10:57 pm
Hi Calliope,
There are a lot of ways a server can be configured and there are a lot of different servers out there with different feature set. Maybe if you describe how it is not working I could better pinpoint what is the problem. Also, provide information like what is the server you are using (apache, IIS, etc). What kind of hosting is it (Windows Server, Linux, etc).
If it is apache, the prime suspect is that local overwrite of server setting is not allowed (.htaccess not allowed and not processed).
Please provide details and maybe I can spot the problem.
June 22nd, 2009 at 11:25 pm
I successfully was able to change access to my blog from faux-beau.com/journal to just faux-beau.com but I would still like to access it from faux-beau.com/journal. You mentioned above that it was possible but I keep getting a 404. Any suggestions?
June 23rd, 2009 at 1:13 am
Kathryn,
I never said you can still access your blog via your old url. However, you will need to access the admin pages with the old url. For example in your case, faux-beau.com/journal/wp-admin.php
June 24th, 2009 at 11:45 pm
It also messes up your RSS feed… at least if you are changing while your blog is already established. Anyway, I found the fix. If you copy/paste the .htaccess and index.php files back into your directory and then update your general wordpress settings to WordPress address and
Blog address to access the directory, but leave the .htaccess and index.php files in the root as well you can access from both faux-beau.com and faux-beau.com/journal
still waiting to see if the RSS feed will update normally again but I’ll let you know when I know
July 11th, 2009 at 5:54 am
I installed my WP in a sub-directory and then just did a redirect of my main URL to that sub. Works fine and I don’t need to lose sleep at night over the .htaccess or index.php file problems. Thanks
July 27th, 2009 at 2:34 am
Hi, I was looking for some solution for my permalinks, the server don´t allow .httacess on root directory (only inside a folder). I have a wordpress directly on root and now I´m messed! Your post cant help on this issue?! Sorry, i´m confused about all that…
August 8th, 2009 at 8:46 pm
Thanks so much, OMG, this is the only tutorial about this that I actually understood! LOL Thanks again! I finally succeeded, check out my link to see my blog working on the root directory, thanks to you. 😉
August 8th, 2009 at 10:49 pm
Love R.F.
I’m surprise that it works for you because the instruction is a bit dated. Especially for the screen shots.
May 27th, 2010 at 7:28 am
Ok, I found a tutorial on WordPress that tells me the exact same thing, however, you have a comment section. Maybe you can help me.
My WordPress files already have their own directory on my server. I moved the two .htaccess file and the index.php file (after changing it) to the root directory. However, http://example.com will not show my website. It goes to the original register.com parking page. The only way to see the website is by going to http://example.com/wordpress. It won’t work any other way. I even experimented with putting the WordPress files in the root directory, but that did me no good.
What am I doing wrong? I followed the instructions exactly, and in fact I was able to make it work for a different website I am working on.
May 28th, 2010 at 3:47 am
I should like to find instructions for doing this that work on a Windows Server (Windows Server 2008 specifically).
It seems in principle that the same set of steps are required, but when the instructions talk specifically about the .htaccess file, that becomes inrrelevant because I don’t have one of those.
I followed the steps as found at the Almighty Codex page at the WordPress web site and it broke my blog (no page displayed). So maybe there are Windows-specific gotchas?
June 13th, 2010 at 4:29 am
I was wondering. If the wordpress files are in http://example.com/wordpress, what should the contents of the robots.txt in the root directory be to prevent robots from accessing the wordpress files.
Should you use:
/wp-content
etc., or should you use
/wordpress/wp-content
etc. in the robots.txt file.
June 23rd, 2010 at 5:43 am
Dear SandboxBlogger,
I have not seen an answer to my previous comment #48.
Could you tell me which is the correct lines to use in the robots.txt file? I think it should be
/wp-content
and not
/wordpress/wp-content
I am not sure because I do not understand how the search engine spider indexes the pages on a website. Does it go to the home pages and uses the hyperlinks to access other pages and posts or does it “see” the directory structure of a website and then access files.
I notice that you use:
User-agent: *
Disallow: /wp/cig-bin/
Disallow: /wp/wp-admin/
Disallow: /wp/wp-content/
Disallow: /wp/wp-includes/
User-agent: Mediapartners-Google*
Disallow: /*/feed/*$
Disallow: /*/trackback/$
User-agent: Googlebot
Disallow: /*/feed/*$
Disallow: /*/trackback/$
User-Agent: msnbot
Disallow: */feed/*$
Disallow: */trackback/$
So you think it should be
/wordpress/wp-content
instead. Can you tell me which is the correct way to use? Thanks.
July 9th, 2010 at 10:40 pm
Hi Han,
I saw your workaround with Michael Aulia and was wondering whether it will work with my situation. I have several blogs running under one hosting with multiple domains. All is redirected but what if I want each blog to only show its own respective domain?
Example:
Domains –
http://www.myblog.com/blog –> only show http://www.blog.com
http://www.myblog.com/blog2 –> only show http://www.blog2.com
http://www.myblog.com/blog3 –> only show http://www.blog.com
Hope you understand what I meant 🙂