Making Mistakes in .htaccess – WordPress at a Subdirectory

WordPress at a Subdirectory Shown in FTP Client

There are essentially 2 ways to set up your WordPress installation at a subdirectory using .htaccess. Those methods are detailed in the Giving WordPress Its Own Directory page in the WordPress Codex.

However, I’ve encountered two different errors because I made a stupid mistake which I’ll detail below.

Basically, all I did was set up my directories as I needed them, then I used the standard WordPress .htaccess and just changed the RewriteBase to be my subdir… Don’t do that! It will appear as though it works, but your post previews, search, and your shortlinks won’t work (i.e. https://megabyterose.com/?p=925). – (I still need to investigate, but this mistake might also cause Password Reset links to not work.)

So since everything looked business-as-usual, I didn’t even realize I had done anything wrong until I was getting a 500 error when using pretty permalinks. The cause of this was because I had a security plugin activated that writes to the .htaccess file (iThemes Security). Basically, the site would be working on the front-end, then I’d visit the admin and pretty permalink pages/posts would throw 500 errors. It was because when the admin was loaded, iThemes Security overwrote everything from the # BEGIN WordPress line to the # END WordPress line in .htaccess – including the RewriteBase (rightfully so!).

So to correct that (and at this point totally forgetting about my previews/search/shortlinks being broken), I just copied the whole WP snippet within .htaccess and changed the RewriteBase in the top one. (Lol now I realize how silly my entire thought process was – I basically just said, “Take that, iThemes!”).

Eventually I realized these issues were related (previews, search, shortlinks, possibly password reset links). – Here are my actual notes I wrote down when it hit me:

Maybe I still did this wrong… Maybe my previews aren’t working because I didn’t set my .htaccess up right…
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory

So once I finally compared my .htaccess with the one from the Codex (Method I), I was just like, “Oh.”

From Liam Kyle Sullivan’s YouTube Video “Shoes”

Standard .htaccess

# 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>

# END WordPress

WordPress at Subdirectory .htaccess (Method I)

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L] 
</IfModule>

As you can see, I assumed I could just change the RewriteBase and call it a day, but there were a lot of other key components to actually make WP work at a subdirectory. So hopefully you can either have a laugh at my mistake or keep this little bit of knowledge in the back of your mind so you don’t make the same assumptions I did. 🙂