问题原文
原帖链接 · billpg · CC BY-SA 4.0
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?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
