• I’ve recently discovered the excellent W3TC plugin as well as the fantastic Roots theme- http://www.rootstheme.com/
    The Roots theme does some really cool things, like cleaning up the pages (including the head section), some best practices taken from the HTML5 Boilerplate and rewrites those pesky wordpress urls like this:
    /wp-content/themes/roots/css/ rewrites to /css/
    /wp-content/themes/roots/js/ rewrites to /js/
    /wp-content/themes/roots/img/ rewrites to /img/
    /wp-content/plugins/ rewrites to /plugins/
    This not only makes the site look less like a WordPress site but also makes the urls shorter.

    However when I select minification and browser caching in W3TC, these changes
    are reversed and it rewrites them to (for example):
    http://myblog.com/wp-content/w3tc/min/6a6a912d.dcd8f1.css

    It would be great if W3 Total Cache could keep the same url structure:
    http://myblog.com/css/6a6a912d.dcd8f1.css
    I know this is possible as this is basically what the Roots theme does (in the functions.php file) but its reversed when W3TC caches the page.

    Is there a way of rewriting the cached pages like the Roots plugin does?

    This is the code that does it in the Roots functions.php file…

    <?php
    if (stristr($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
    function roots_htaccess_writable() {
    if (!is_writable(get_home_path() . '.htaccess')) {
    add_action('admin_notices', create_function('', "echo '” . sprintf(__(‘Please make sure your .htaccess file is writeable ‘, ‘roots’), admin_url(‘options-permalink.php’)) . “‘;”));
    };
    }
    
    add_action(‘admin_init’, ‘roots_htaccess_writable’);
    
    // Rewrites DO NOT happen for child themes
    // rewrite /wp-content/themes/roots/css/ to /css/
    // rewrite /wp-content/themes/roots/js/ to /js/
    // rewrite /wp-content/themes/roots/img/ to /js/
    // rewrite /wp-content/plugins/ to /plugins/
    
    function roots_flush_rewrites() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
    }
    
    function roots_add_rewrites($content) {
    $theme_name = next(explode(‘/themes/’, get_stylesheet_directory()));
    global $wp_rewrite;
    $roots_new_non_wp_rules = array(
    ‘css/(.*)’ => ‘wp-content/themes/’. $theme_name . ‘/css/$1′,
    ‘js/(.*)’ => ‘wp-content/themes/’. $theme_name . ‘/js/$1′,
    ‘img/(.*)’ => ‘wp-content/themes/’. $theme_name . ‘/img/$1′,
    ‘plugins/(.*)’ => ‘wp-content/plugins/$1′
    );
    $wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
    }
    
    add_action(‘admin_init’, ‘roots_flush_rewrites’);
    
    function roots_clean_assets($content) {
    $theme_name = next(explode(‘/themes/’, $content));
    $current_path = ‘/wp-content/themes/’ . $theme_name;
    $new_path = ”;
    $content = str_replace($current_path, $new_path, $content);
    return $content;
    }
    
    function roots_clean_plugins($content) {
    $current_path = ‘/wp-content/plugins’;
    $new_path = ‘/plugins’;
    $content = str_replace($current_path, $new_path, $content);
    return $content;
    }
    
    // only use clean urls if the theme isn’t a child or an MU (Network) install
    if (!is_multisite() && !is_child_theme()) {
    add_action(‘generate_rewrite_rules’, ‘roots_add_rewrites’);
    if (!is_admin()) {
    add_filter(‘plugins_url’, ‘roots_clean_plugins’);
    add_filter(‘bloginfo’, ‘roots_clean_assets’);
    add_filter(‘stylesheet_directory_uri’, ‘roots_clean_assets’);
    add_filter(‘template_directory_uri’, ‘roots_clean_assets’);
    }
    }
    
    function roots_add_h5bp_htaccess($rules) {
    global $wp_filesystem;
    
    if (!defined(‘FS_METHOD’)) define(‘FS_METHOD’, ‘direct’);
    if (is_null($wp_filesystem)) WP_Filesystem(array(), ABSPATH);
    
    if (!defined(‘WP_CONTENT_DIR’))
    define(‘WP_CONTENT_DIR’, ABSPATH . ‘wp-content’);
    
    $theme_name = next(explode(‘/themes/’, get_template_directory()));
    $filename = WP_CONTENT_DIR . ‘/themes/’ . $theme_name . ‘/inc/h5bp-htaccess’;
    
    $rules .= $wp_filesystem->get_contents($filename);
    
    return $rules;
    }
    
    add_action(‘mod_rewrite_rules’, ‘roots_add_h5bp_htaccess’);
    }
    
    ?>

    and here is the .htaccess code:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteRule ^css/(.*) /wp-content/themes/retlehs-roots-372a768/css/$1 [QSA,L]
    RewriteRule ^js/(.*) /wp-content/themes/retlehs-roots-372a768/js/$1 [QSA,L]
    RewriteRule ^img/(.*) /wp-content/themes/retlehs-roots-372a768/img/$1 [QSA,L]
    RewriteRule ^plugins/(.*) /wp-content/plugins/$1 [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    I emailed W3 Edge, and it seems that the above functionality isn’t on the roadmap. I would be so grateful if there was a way to fix this!

Viewing 1 replies (of 1 total)
  • Plugin Contributor Frederick Townes

    (@fredericktownes)

    Boilerplate is actually tidying up things that W3TC can take care of, as a result, there’s a bit of an impasse. If you prefer one, use that, I don’t recommend it, because you will have to maintain that implementation while W3TC instead works to behave “natively” with WP.

Viewing 1 replies (of 1 total)

The topic ‘URL Rewrites and the Roots Theme’ is closed to new replies.