Score:0

.htaccess for Wordpress in a subfolder

th flag

I have a setup where a static website sits in the root folder and Wordpress is located in a subfolder. So I need to rewrite all requests to index.html while all Wordpress-related requests should go to the subfolder. I'm achieving this using two .htaccess files: one in the root and another in Wordpress subfolder.

Root .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/wp-.+
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteRule (wp-.*)$ /wordpress/$1 [L]
# Rewrite rule for frontend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

Wordpress subfolder .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

This works but when I try to openning /wp-admin/ I get redirected to /wp-login.php?redirect_to=https%3A%2F%2Fwww.example.com%2Fwordpress%2Fwp-admin%2F&reauth=1 so the subfolder is exposed in the URL.

Is there a way to hide the subfolder?

kz flag
What format do your WordPress URLs take? From your rules, it looks like they all have a `wp-` prefix?! (Although the regex is not anchored, so that is unclear.) Does that not already expose the `/wordpress` subdirectory when referencing your static assets (unless these also include a `wp-` prefix)? But if you are using a `wp-` prefix, why not just use a `/wp/` subdirectory? "I get redirected to" - This is an issue with your WordPress config (not `.htaccess`) so is off-topic on ServerFault. See https://wordpress.stackexchange.com/
Score:-1
nl flag

To hide the subfolder in the URL for WordPress-related requests, you can modify your setup slightly. You can keep the root .htaccess file as it is, but make some changes to the .htaccess file in the WordPress subfolder. Here's how you can do it:

  1. Modify the .htaccess file in the WordPress subfolder (e.g., /wordpress/) as follows:
RewriteEngine On RewriteBase /wordpress/ RewriteRule ^index\.php$ - [L]

Add this line to prevent exposing the subfolder in the URL

RewriteCond %{REQUEST_URI} !^/wordpress/

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /wordpress/index.php [L]

By adding the RewriteCond %{REQUEST_URI} !^/wordpress/ condition, you're ensuring that requests to the /wp-admin/ folder won't be affected by this rewrite rule, so the subfolder won't be exposed in the URL for WordPress-related requests.

  1. In your root .htaccess file, you can simplify the rules for rewriting to index.html:

    RewriteEngine On

    Exclude requests to the /wordpress/ subfolder from this rule

    RewriteCond %{REQUEST_URI} !^/wordpress/

    Rewrite rule for frontend

    RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L]

With these modifications, WordPress-related requests will still be handled by the WordPress subfolder, and the subfolder won't be exposed in the URL when accessing /wp-admin/. Other requests will be rewritten to index.html in the root folder as before.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.