In this example of subdirectory redirection, we’ll be using .htaccess to properly redirect a website address such that both the website and WordPress dashboard load without the subdirectory name.
Our domain for this example will be “your-domain-name.com”
Within the public directory, often named public_html or html, you should see the following settings for a standard WordPress permalinks setup:
# 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] </IfModule>
To enable your website to function properly within your blog subdirectory, but without showing the blog subdirectory name, we actually need to change three different files, shown in the picture at right:
The first file we’ll edit is the .htaccess file (contents shown above), to:
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} ^(www.)?your-domain-name.com$ RewriteCond %{REQUEST_URI} !^/blog/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /blog/$1 RewriteCond %{HTTP_HOST} ^(www.)?your-domain-name.com$ RewriteRule ^(/)?$ /blog/index.php [L] </IfModule>
The second will be the index.php file, from:
/** Loads the WordPress Environment and Template */ require( dirname( __FILE__ ) . '/wp-blog-header.php' );
to:
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/blog/wp-blog-header.php' );
And lastly, open the blog directory, then edit the .htaccess file within to:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /blog/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] </IfModule> # END WordPress
Updating these three files, as shown above, will accomplish two goals:
I hope you’ve found this short tutorial on how to set up a WordPress site within a subdirectory without displaying the subdirectory within Google search or the WordPress dashboard helpful.
If you have further questions please feel free to leave your comments or questions below.
Enjoy!