Wordpress - Masking wp-content/themes/name/images to just images directory using htaccess

Check out the Roots WordPress Theme. They seem to do exactly what you want with the URLs.

Here's a snippet from their roots-htaccess.php file:

add_action( 'generate_rewrite_rules', 'roots_add_rewrites' );

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',
  );
  $wp_rewrite->non_wp_rules += $roots_new_non_wp_rules;
}

Note: if you can pull this off directly in a .htaccess file, as in ZweiBlumen's answer, you should pick that solution since it most probably is more optimized.


If the issue is only with images, but not css or javascript, I think there's a typo in your RewriteRule. I think your missing a "1" after the "$":

RewriteRule ^images/(.*)$ wp-content/themes/standard/images/$1 [L]

Also, you might want to try putting those extra statements below the initial rule, ie below this line:

RewriteRule ^index\.php$ - [L]

Not sure though.


Why don't you create a shortcode for that in the following manner.

function img_folder_shortcode( ) {
   return get_stylesheet_directory_uri() . '/images';
}
add_shortcode( 'img_folder', 'img_folder_shortcode' );

And then use the following shortcode anywhere in the content area.

[img_folder]/img.jpg
<img src="[img_folder]/img.jpg" alt="img" />