Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

It's possible to move wp-config.php up one directory level for security, i.e. into the directory above public_html (sometimes called home or a user name, depending on the web host).

"You can move the wp-config.php file to the directory above your WordPress install", according to http://codex.wordpress.org/Hardening_WordPress#Securing_wp-config.php And I have done that with one WP sites' wp-config.php file.

But how does one do that with multiple WP sites in their own folders in public_html? Is there a way to rename multiple wp-config.php files - something like wp-config1.php and wp-config2.php, etc. - in the home directory for each different WP site? And use .htaccess or some other method to enable each WP install to find its own wp-config.php file?

One solution I guess is Multisite, but that's not possible in this case.

share|improve this question
    
What do you mean "for security"? No one can read your wp-config.php file through the browser anyway ... –  EAMann Feb 13 '12 at 18:15
    
    
Read that same note very carefully ... it talks about moving wp-config.php to the same level as /wp-includes ... not to the level of /home below /public_html. You're moving things in the opposite direction. –  EAMann Feb 13 '12 at 19:02
    
Read what I wrote and the WP docs: "up one directory level..." That's one level above /wp-includes/. That's what works right now. If I moved wp-config.php into wp-admin or another folder, it wouldn't work. –  songdogtech Feb 13 '12 at 21:25
    
My apologies. Was reading too quickly before lunch and misinterpreted that Codex note. See my answer below. –  EAMann Feb 13 '12 at 21:41

3 Answers 3

Use one main wp-config.php and name the others according to the name of the host:

  • example.com.config.php
  • example.net.config.php
  • and so on.

In your main wp-config.php write:

$current_config = __DIR__ . '/' . $_SERVER['HTTP_HOST'] . '.config.php';
if ( file_exists( $current_config )
    require_once $current_config;
share|improve this answer
    
Thanks, I'll have to try this during the next few days. –  songdogtech Feb 16 '12 at 2:29

You could do this:

Copy all the wp-config.php files into the level above the installs, then rename them to something like mysite-wp-config.php and then inside the actual installs, you can have a file called wp-config.php then do this inside that file:

<?php
    include_once(‘/pathtoconfigfile/mysite-wp-config.php’);
?>

This is a theory, I have not tested it.

share|improve this answer

There are two ways to do this. From your question, it sounds like you have this structure currently:

/home
... /public_html
... ... wp-config.php <- for site1
... ... /site1
... ... ... /wp-admin
... ... ... /wp-content
... ... ... /wp-includes
... ... /site2
... ... ... /wp-admin
... ... ... /wp-content
... ... ... /wp-includes
... ... ... wp-config.php <- for site2

And what you want to do is also move the wp-config.php file for your other sites out of the site root as well. You're correct in pointing out that this will lead to some conflicts because every site will try to include wp-config.php but can't all use the same one.

The bigger problem is that there is no way to use wp-config.site1.php or wp-config1.php or any other variation to keep things separate without hacking core.

If you take a look at the code in wp-load.php you'll see this:

if ( file_exists( ABSPATH . 'wp-config.php') ) {

    /** The config file resides in ABSPATH */
    require_once( ABSPATH . 'wp-config.php' );

} elseif ( file_exists( dirname(ABSPATH) . '/wp-config.php' ) && ! file_exists( dirname(ABSPATH) . '/wp-settings.php' ) ) {

    /** The config file resides one level above ABSPATH but is not part of another install*/
    require_once( dirname(ABSPATH) . '/wp-config.php' );

}

WordPress will only look one directory past its current location and will only look for wp-config.php.

Your best bet would be to push everything up one level further to give yourself some separation. Then point your Apache vhosts file at the new folder locations. So set your directory structure up like this:

/home
... /public_html
... ... /site1
... ... ... wp-config.php <- for site1
... ... ... /wordpress
... ... ... ... /wp-admin
... ... ... ... /wp-content
... ... ... ... /wp-includes
... ... /site2
... ... ... wp-config.php <- for site2
... ... ... /wordpress
... ... ... ... /wp-admin
... ... ... ... /wp-content
... ... ... ... /wp-includes

Your vhosts file will then point www.site1.com to /home/public_html/site1/wordpress instead of /home/public_html/site. Yes, it's a bit more convoluted, but the only way you can avoid this issue.

share|improve this answer
    
Thanks for this extensive answer, and this should work fine on a VPS or other machine where one has access to vhosts, but I'm on a good - but shared - system. –  songdogtech Feb 16 '12 at 2:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.