Home  >  Q&A  >  body text

Add subdirectory to WP-CONTENT URL using HTACCESS in WordPress multisite

I am converting my single site WordPress installation to a multisite. I'm trying to fix broken CSS/JS on my main site.

I currently have two websites on my network:

My multisite installation is located in a subdirectory we call "wordpress". So the file path looks like public_html/wordpress.

My goal is that neither website's URL contains the "wordpress" subdirectory. Everything seems to be working fine except for broken CSS and JS on the primary site (the secondary site looks fine).

When inspecting the code, all CSS and JS calls point to http://www.example.com/wp-content/, but the files are not found there. If I go to http://www.example.com/wordpress/wp-content in my browser, I will find these files. I want to hide the wordpress folder but still be able to retrieve the files.

I'm confused on how to set up the HTACCESS file. I've made some preliminary changes to it to make multisites in subdirectories work properly. These are the guides I found on StackOverflow and other sites online on how to move a site to a multisite with subdirectories and hide the subdirectories. I haven't found any information on fixing CSS/JS broken issues.

I think I need to update one or more of the 3 HTACCESS files.

1.) public_html/.htaccess

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

2.) public_html/wordpress/.htaccess

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

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)  [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$  [L]
RewriteRule . index.php [L]
</IfModule>

3.) public_html/wordpress/wp-content/.htaccess The file does not exist, but I created it. My idea is to call the files without the wordpress subdirectory, but they need to behave as if they contain the subdirectory. For example, currently http://www.example.com/wp-content/uploads/image.jpg is broken, but http://www.example.com/wordpress/wp-content/uploads/image.jpg works. I want it to be the other way around, or I want both paths to work.

<IfModule mod_rewrite.c>
RewriteEngine on
# ADD WORDPRESS IF URL DOES NOT HAVE IT
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteRule ^(.*)$ /wordpress/
</IfModule>

I tried adding different lines to various HTACCESS files but none of them worked. I'm also unsure of the line number where the new rule should be inserted. One of my new rules may be correct, but in the wrong place. Here's one that I really thought would work but didn't.

RewriteRule ^/wp-content/(.*)$ /wordpress/wp-content/ [R=301,NC,L]


P粉917406009P粉917406009265 days ago821

reply all(1)I'll reply

  • P粉930448030

    P粉9304480302024-01-03 12:56:22

    First, you should delete all .htaccess files except the one in the root directory: public_html/.htaccess

    Secondly, your last rule doesn't work because it's slightly wrong.

    You should change it to:

    RewriteRule ^/wp-content/(.*)$ /wordpress/wp-content/ [R=301,NC,L]

    To:

    RewriteRule ^wp-content/(.*)$ wordpress/wp-content/ [L,NC]

    Because you don't need the starting /, and you don't need a 301 redirect. You want to hide the wordpress folder and map the requested URL from wp-content/(.*) to wordpress/wp-content/$1 code>

    Additionally, this rule must be the first rule in the .htaccess file, taking precedence over the following default WordPress rules. The final and only .htaccess from public_html/ should look like this:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^wp-content/(.*)$ wordpress/wp-content/ [L,nc]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    Hope it helps you.

    reply
    0
  • Cancelreply