0

I have a semi-bespoke scenario, but one that works best for out environment sharing the code workload and accessibility (and version control).

We have a multisite that all abide by the same core styles, scripts, fonts, and images.

We converted those out of a parent theme into a mu-plugin as the plugin handles a lot more than those (activation, API functions, etc.)

Previously, we were using a standard Wordpress install and had .htaccess doing the rewrite which was fine:

<IfModule mod_rewrite.c>
...
# rewrites
RewriteRule ^css/(.*)   wp-content/mu-plugins/core/resources/css/$1  [QSA,L]
RewriteRule ^font/(.*)  wp-content/mu-plugins/core/resources/font/$1 [QSA,L]
RewriteRule ^img/(.*)   wp-content/mu-plugins/core/resources/img/$1  [QSA,L]
RewriteRule ^js/(.*)    wp-content/mu-plugins/core/resources/js/$1   [QSA,L]
...
</IfModule>

But I was wondering is there a way to do this without having to edit the .htaccess file? I looked into endpoints thinking I could get the files as:

http://domain.ltd/core/css/file.css
http://domain.ltd/core/font/file.svg
http://domain.ltd/core/img/file.img
http://domain.ltd/core/js/file.js

The aim is that once you install the plugin you didn't have to worry about the htaccess file the plugin itself handled the routing for the url and prettied.

  • I realized that I forgot to add core/ to the rewrite rule pattern, i.e. core/(css|font|img|js); but, have you tried the solution, or did I misunderstand the question? – Sally CJ Oct 5 at 4:29
0

There is, and it would be using the parse_request hook. For example in your case:

add_action( 'parse_request', 'my_parse_request' );
function my_parse_request( $wp ) {
    if ( preg_match( '#^(css|font|img|js)/(.+)$#', $wp->request, $m ) ) {
        $file = WPMU_PLUGIN_DIR . '/core/resources/' . $m[1] . '/' . $m[2];
        if ( $mime = mime_content_type( $file ) ) {
            header( "Content-Type: $mime" );
            readfile( $file );
            exit;
        }
        // else, up to you..
    }
}

But it seems like using the htaccess is better for serving the static assets, and you can actually use the WordPress rewrite API to dynamically write to the htaccess file, i.e. without manually opening and editing the file. So for example, you can do something like:

add_action( 'init', 'my_init' );
function my_init() {
    add_rewrite_rule(
        '(css|font|img|js)/(.+)$',
        'wp-content/mu-plugins/core/resources/$1/$2',
        'top'
    );
}

And then make sure to flush the rewrite rules — just visit the permalink settings admin page (wp-admin → Settings → Permalinks).

| improve this answer | |

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.