0

Currently I'm developing a theme based on Angular 5. Angular exports static assets into a subfolder assets. My development wordpress installation is located in localhost/wordpress. When using the theme it tries to access them from this url localhost/wordpress/assets. This fails because the assets folder is located here: localhost/wordpress/wp-content/themes/gruppe-adler/dist/assets/ where gruppe-adler is the name of the theme.

My approach is to add a custom rewrite rule in my theme's functions.php to redirect all requests to the right folder.

This is the code I used for this purpose

function assets_init($rules) {
    $assetsRule = array('^assets/([^/]+)/?$' => 'wp-content/themes/gruppe-adler/dist/assets/$matches[1]');
    return array_merge($assetsRule, $rules);
}
add_filter('page_rewrite_rules', 'assets_init');

I hardcoded the path for testing purposes because I was unable to find a function that just returns the relative url of my theme folder. However, this doesn't work. I used a rewrite analyzer plugin to check if it is being registered. It is registered but gives the error This query variable is not public and will not be saved.

enter image description here

This is confusing as there is no query variable involved in my rewrite rule. Also, the double point at the end is strange.

My main goal is to achieve a redirect to this assets which means a redirect to a file on the filesystem. This is not about query params or some redirect to the index.php. What am I missing?

2

The wordpress "rewrite rules" have a horrible name which do not match what people are expecting it to be. Those are not rules to rewrite or redirect or whatever other things you can do with htaccess, but rules which are used to "rewrite" urls into the relevant WP_Query parameters. Therefor if you have a rewrite rule that do not end up with WP_Query arguments, you are using the wrong tool for whatever you are trying to do.

To go deeper into what you are trying to do.... just don't. It is not up to the plugin or theme author to decide how the urls should look. Simple example for fail point - sites with no mod_rewrite and therefor no "pretty url" handling at all. You need to figure how to properly configure angular to your specific setting.

3
  • Means this maybe has to be handled by external rewrite rules e.g. in .htaccess? Mar 7 '18 at 19:48
  • yes, and wordpress also has API for that,(modify the htaccess) but as I said, you are going to end assuming things about the location of files, so not sure at all that this is a direction you should follow. With htacces you also not compatible to ngix and IIS Mar 7 '18 at 19:56
  • I actually managed to hack the redirect into my template. My approach is to use a rewrite that provides the full url as a url param, check in index.php if this url param exists and then just redirect to this url by header. Mar 8 '18 at 1:16

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.