How to Serve Wordpress Site from Subdirectory

Wordpress by default is installed in the root directory var/www/ This causes a lot of inconvenience as often we need to install other files into separate subdirectories under root directory. A good installation practice should keep every project in its own subdirectory.

Many people try to do this in various ways. Due to different end goals, all these guides and tutorials lead to varied results that don't quite suit my needs.

Here's what I need to accomplish:

The wordpress files will be served from URL from subdirectory var/www/ All the post URLs should preserve the original format as, which is crucial for SEO (wrong setup would change URL to

Also, other projects should be served from their own subdirectories in root without being affected. For example, will be served from URL from subdirectory /var/www/

Through series of experiments, the below configuration is found to work for my goals perfectly.

Move wordpress into its own subdirectory

Move all these wordpress core files from var/www/ to var/www/


Create a .htaccess in the root directory

First we need to set up .htaccess to preserve the URLs in the right format. Apache's rewrite module can achieve this.

Create a .htaccess file in the root directory /var/www/

cd /var/www/
vim .htaccess

Copy and paste this into the file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Check the apache configuration to make sure this module is installed:

cd /etc/apache2/mods-enabled/

If it's not there, enable it and restart apache

sudo a2enmod rewrite
sudo service apache2 restart

Wordpress has an official guide on how to edit this .htaccess in root. It's different from the one I use above though. Not sure if it'll serve the same purpose. I'll stick with mine that's proved to work.

Copy index.php into the root directory

Now we need to load up wordpress files from the root directory.

cd /var/www/
cp wordpress/index.php .

Add a wordpress (the name of the subdirectory for wordpress files) into the last line of this file. After the edit, this file should look like this (comments removed for cleaner display):

define('WP_USE_THEMES', true);
require( dirname( __FILE__ ) . '/wordpress/wp-blog-header.php' );

Define SITEURL in wp-config.php

Add these two lines into wordpress/wp-config.php

/** Set wordpress directory path and site url */
define('WP_HOME', '');
define('WP_SITEURL', '');

That should do the trick. To see a live demo, take a look at: (wordpress-based)

and (jQuery-based).

Go Top
comments powered by Disqus