2

I have an established website with some legacy folders I would like to keep.

  • https://example.com/.well-known/
  • https://example.com/Music/
  • https://example.com/Rutabagas/

I've also installed WordPress. Because there's an automated update/backup system, I chose to put WordPress into a folder. This way, the WordPress management system won't see my legacy folders as part of WordPress and wipe them out next time there's an upgrade.

  • https://example.com/WordPress/

I would like for when anyone visits https://example.com/ to see the WordPress site, without the /WordPress/ part of the URL. No problem, Google has several results on how to do this, except, I still want my list of legacy folders to keep working and the answers I can find only work to redirect everything. https://wordpress.org/support/article/giving-wordpress-its-own-directory/

Is there a way to configure .htaccess to rewrite everything into the WordPress folder including /, but not my three or four legacy folders and everything inside, which I want the server to continue handling as normal?

  • I don't think that a word press upgrade would ever delete your legacy files. It is very common for WordPress to be installed along with static HTML and other resources. I'd consider just moving WordPress to the main directory and making sure you have backups of your old files. – Stephen Ostermiller Sep 24 at 10:28
  • @StephenOstermiller Fair enough, but since my long term plan is to automate all upgrades and backups, I would prefer to adhere to the principle of minimum surprise and say that everything under a folder is WordPress. – billpg Sep 24 at 13:01
1

If you've followed "Method I" on the WordPress help page you link to then you shouldn't actually need to do anything else. Your legacy folders are already excluded.

The solution they give is (.htaccess in the root directory):

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

This already excludes any existing files and directories from being rewritten. The two conditions that check against REQUEST_FILENAME handle this. So, any legacy folders on your filesystem are already excluded.

The only thing that is not excluded is if you requested a non-existent file in a legacy folder. Currently that would be routed through WordPress and result in a WordPress generated 404. This is probably desirable I would think. However, if you wish to exclude any request to the legacy folder then you can add additional conditions to the above rule block...

RewriteCond %{REQUEST_URI} !^/my_subdir/

This specifically excludes any request that is already for the /my_subdir/ directory (ie. the directory that contains your WordPress installation). (Although this check is actually unnecessary if you have an additional .htaccess file in the WordPress subdirectory and pretty permalinks enabled.). Simply repeat this condition for each legacy folder you wish to exclude:

 RewriteCond %{REQUEST_URI} !^/\.well-known/
 RewriteCond %{REQUEST_URI} !^/Music/
 RewriteCond %{REQUEST_URI} !^/Rutabagas/

Explicitly excluding these folders in this way is actually more efficient, since it avoids the filesystem check that follows.

NB: The dot (.) is a meta character in the regex and needs to be backslash escaped in order to match a literal dot only. The WordPress code sample above actually failed to do this...

RewriteCond %{HTTP_HOST} ^(www.)?example.com$

Should be:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$

Although, if you are on shared hosting and you only have 1 domain name, then this HTTP_HOST check is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.