WordPress Subdirectory Setup for Root Domain
This guide assumes your WordPress installation is in a subdirectory (e.g., public_html/blog/
) and you want your main domain (www.yourdomain.com
) to display the content from that subdirectory.
Step 1: Configure WordPress General Settings
Log in to your WordPress admin panel (it will be at http://www.yourdomain.com/blog/wp-admin/ initially).
Go to Settings > General.
- WordPress Address (URL): Enter the full URL to your WordPress installation directory.
- Example:
http://www.yourdomain.com/blog
- Example:
- Site Address (URL): Enter the URL you want visitors to type to reach your site (your main domain).
- Example:
http://www.yourdomain.com
- Example:
Important: After saving these changes, your WordPress admin area might seem broken or redirect incorrectly. This is expected until the .htaccess
and index.php
changes are made.
Step 2: Copy index.php
and .htaccess
to Root
Using an FTP client or your hosting file manager:
- Copy (do NOT move) the
index.php
file from your WordPress subdirectory (public_html/blog/index.php
) to your main domain’s root directory (public_html/index.php
). - Copy (do NOT move) the
.htaccess
file from your WordPress subdirectory (public_html/blog/.htaccess
) to your main domain’s root directory (public_html/.htaccess
).- If a
.htaccess
file already exists in your root, you will need to combine the rules (see Step 4). - Remember that
.htaccess
files are often hidden, so ensure your FTP client or file manager is set to show hidden files.
- If a
Step 3: Modify index.php
in the Domain Root
Open the index.php
file you just copied to your domain root (public_html/index.php
) in a text editor.
Find this line (usually near the bottom):
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
Change it to include your subdirectory name:
require( dirname( __FILE__ ) . '/blog/wp-blog-header.php' ); // Replace 'blog' with your actual subdirectory name
Save the changes to this index.php
file.
Step 4: .htaccess
in the Domain Root (public_html/.htaccess
)
This file tells your server to serve content from the WordPress subdirectory when the main domain is accessed.
Add these rules to your root .htaccess
file. If you already have other rules, place these before any other WordPress-specific rules (like the BEGIN WordPress
block if it was already there).
# Custom rules for WordPress in subdirectory <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # Redirect requests for the subdirectory itself to the root (optional, but good for SEO) # RewriteRule ^blog/?$ / [R=301,L] # Uncomment and adjust if you want /blog to redirect to / # Rewrite all requests to the WordPress index.php in the subdirectory # unless they are for actual files or directories in the root RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] # Replace 'blog' with your actual subdirectory name </IfModule> # BEGIN WordPress # The directives (lines) between "BEGIN WordPress" and "END WordPress" are # dynamically generated, and should only be modified via WordPress filters. # Any changes to the directives between these markers will be overwritten. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress
Explanation of Root .htaccess
:
RewriteEngine On
: Activates the rewrite engine.RewriteBase /
: Sets the base URL for rewrite rules to the root of your domain.RewriteCond %{REQUEST_FILENAME} !-f
: Ensures the rule doesn’t apply if the request is for an actual existing file in the root.RewriteCond %{REQUEST_FILENAME} !-d
: Ensures the rule doesn’t apply if the request is for an actual existing directory in the root.RewriteRule . /blog/index.php [L]
: This is the core rule. If the request isn’t for an existing file or directory, it internally rewrites the request to theindex.php
file inside your WordPress subdirectory.[L]
means “last rule,” stopping further processing.- The
# BEGIN WordPress
and# END WordPress
block is the standard WordPress block. EnsureRewriteBase /
is set here.
Step 5: .htaccess
in the WordPress Subdirectory (public_html/blog/.htaccess
)
This file should contain the standard WordPress permalink rules, but with the RewriteBase
correctly set to its own subdirectory. WordPress usually generates this automatically.
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /blog/ # This MUST be your subdirectory name with a trailing slash RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] # This MUST also reflect your subdirectory name </IfModule> # END WordPress
Explanation of Subdirectory .htaccess
:
RewriteBase /blog/
: This is critical for WordPress to correctly generate permalinks and handle internal rewrites for its own files. It tells WordPress that its “root” for permalink purposes is/blog/
.- The rest are standard WordPress permalink rules, ensuring your posts, pages, and assets load correctly within the subdirectory.
Step 6: Update Permalinks in WordPress
Even if you don’t change anything, go to your WordPress admin panel (http://www.yourdomain.com/wp-admin/
should now work).
Navigate to Settings > Permalinks.
Simply click the “Save Changes” button without making any modifications. This action forces WordPress to refresh and rewrite its .htaccess file in the subdirectory, ensuring all paths are correctly registered.
Final Checks:
- Clear Browser Cache: Your browser might cache old redirects. Clear your browser’s cache or try an incognito/private window.
- Test All Links: Ensure your homepage, posts, pages, images, and other assets load correctly.
- Check for Conflicts: If you have other applications or custom
.htaccess
rules in your root, they might conflict. Adjust the order or conditions as necessary.