Search code examples
wordpressapache.htaccessurl-rewritingmultisite

Add trailing slashes in WordPress Multisite without damaging the admin interface or the JSON API


For a WordPress multisite installation I am having the following part of my .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)([^./])$ %{REQUEST_URI}/ [L,R=301,NE]

In a single WordPress installation where /wp-admin and /wp-json always follow directly after the domain I can exclude those from the trailing slash rule with this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wp-admin
RewriteCond %{REQUEST_URI} !^/wp-json
RewriteRule ^ / [R=301,L]

In a WordPress multisite the Admin interface and the JSON API can be reached like this:

www.example.com/wp-admin
www.example.com/wp-admin/whatever
www.example.com/site1/wp-admin
www.example.com/site2/wp-admin/whatever?whatever

www.example.com/wp-json
www.example.com/wp-json/whatever
www.example.com/site1/wp-json
www.example.com/site2/wp-json/plugin-7/12/request

and so on...

What is the correct .htaccess rule for putting this together? Have tried certain things without success. Not necessarily a WordPress specific question.


Solution

  • Nevermind I have finally figured it out:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/wp-json
    RewriteCond %{REQUEST_URI} !^/wp-admin
    RewriteCond %{REQUEST_URI} !^/([_0-9a-zA-Z-]+)/wp-json
    RewriteCond %{REQUEST_URI} !^/([_0-9a-zA-Z-]+)/wp-admin
    RewriteRule ^([^.]+)([^./])$ %{REQUEST_URI}/ [L,R=301,NE]