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>

Removing the subdirectory blog from your URLThe above works perfectly fine when your main domain name is located at the top of your public directory–not so well if your site is located in a subdirectory like “blog

 

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:

Numero 1Display your website in the web browser location bar as the domain name only and not show the trailing subdirectory /blog

Numero 2And allow you to access your WordPress dashboard as simply www.your-domain-name.com/wp-admin/ instead of www.your-domain-name.com/blog/wp-admin/

 

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!

 

3 Responses

    1. I did find this useful, thank you. For modifying an existing site with subdirectory (/wp/) “showing,” to hiding the sub, I wasn’t able to infer the right info from the Codex page about subdirectory setup. Turned out our docroot index.php and the wp/.htaccess were good to go as they were. (I had the docroot .htaccess correct from reading the Codex page.)

      One quirk remains, however. Our wp-admin and such remain where they were with the /wp/ subdirectory showing. We weren’t looking to “accomplish the goal” #2 shown above, but I did kind of expect that to happen.

      We changed from http to all https at the same time (having previously set the wp-admin to use https, in wp-config.php) and I the rendered page source has all site urls https… but resource links to css and js still have URLs with the /wp/ in them. We did modify our database (properly) to change all instances of http://domainname/wp/ to https://domainname/

Please feel free to comment via WordPress, Twitter, or Facebook

Proactive WordPress Security Management for Pennies a Day™
© Copyright 2024 HackGuard.com™, HackRepair.com™,
The Hack Repair Guy™, Hack Repair Guy™
Copyright and Trademark Statement | Privacy Policy

Call HackRepair.com for website security help, (619) 479-6637.
Content Approved By Jim Walker, The Hack Repair Guy