The random eclecticisms of loonytoons

Magento – changing the root folder after install

Current magento version –

I’m guessing that quite a few people will have done what I did when I initially set up Magento, which was to just move the magento folder into my required document root and then go in and run the installer.
Logically all the files then got installed into that magento folder resulting in all my urls having “/magento/” in them which wasn’t exactly ideal. Of course this fact only registered with me once I’d done quite a bit of work, so the idea of doing a fresh install to sort this out was extremely unappealing to me.

Luckily for me it is possible to change the magento root from “/magento/” to “/”, but as you might imagine this sort of thing is fraught with danger (if that’s not too dramatic).

So I will stress here that it’s best to setup your root folders correctly on install, or run a fresh install if you can. But if you really need to change the root after the event then here’s what you have to do:

  • Move all you files into your new document root (so out of the magento folder).
  • In the magento admin panel, go to System->Configuration->Web and change the URL’s in the ‘Unsecure’ and ‘Secure’ tabs to take account of this change.

These steps should allow to access your site using the new root folder and URLs, so far so easy. However you will also need to change the pear setup to ensure that any extensions you download will get installed in the right place. So:

  • Open up the downloader/pearlib/pear.ini. Search this file for all file/folder references and change these so that they match your new folder structure. NOTE: you will also have to change the number that appears infront of each reference as this needs to match the number of characters in the string. So in my case I had to take off 8 each time as I was always removing “magento/” from the string.
  • I also changed the following files as they also referenced the old root:
    • downloader/pearlib/php/pearcmd.php
    • downloader/pearlib/php/peclcmd.php
    • downloader/pearlib/pear
    • downloader/pearlib/peardev
    • downloader/pearlib/pecl

Obviously this course of action is open to many possible mistakes and errors as you have to make sure that all the files are moved and referenced correctly, and if something goes wrong it is very hard to try and track it down.

So at the risk of repeating myself one last time it’s definitely best to do a clean install if possible, but if done correctly this should work.
Just remember to do a full backup first!


4 thoughts on “Magento – changing the root folder after install

  1. Thank you! This saved me having to figure out why the Magento Downloader was trying to update files in the wrong directory.

  2. tavakoli says:

    ho to change root file patch from /home/domains/domainname/public_html to / in templates in magento?

  3. loonytoons says:

    Hi Tavakoli,
    I’m not entirely sure what it is that you are trying to do.

    If you’re changing the root folder then the instructions above should work. If you’re having problems with the pear.ini file then apparently it is possible to regenerate the whole file once you’ve changed the root folder, instructions here –

    You can check what the base magento file path is though by calling Mage::getBaseDir(‘base’);
    Or you can get the URL info in the template, e.g. Mage::getBaseUrl(), or $this->getSkinUrl();
    Double check your .htaccess file has been copied over. Also double check that your file permissions are set correctly so that the webserver can read the files.

    If none of that helps then can you be more specific about what the problem is that you’re having?

  4. Nicolas says:

    I’ve installed my magento files in the root directory however I prefer my site files to be encapsulated in their own directory.

    I wish to move all these files into a directory (let’s call it store) BUT I want to keep the URL structure. AS FAR AS THE USER IS CONCERNED the site resides in the root directory. I don’t want to see ‘store’ or index.php anywhere in my file references

    I have done this for my previous Magento 1.9.x install but for some reason it’s proving difficult in Magento 2

    I prefer it this way because i’m not mixing any additional files with my Magento site files through the life of the application.
    – Whenever I make a backup it easy to just zip the ‘store’ file after adding my mySQL dump and downloading the whole zipped site to a secure HD
    – If I include a Blog that resides in its own directory as well
    Call me pedantic but this is the way I have always written code and I like to keep all my files as clean and encapsulated as my code

Leave a Reply

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