I’m trying what I thought would be something really simple of masking my url but cannot seem to get it to work. I want to be able to link to images in my img tag without having to type in the full url.

i.e.

Current url:  http://server.com/wp-content/themes/standard/images/img.jpg
or
<img src = "http://server.com/wp-content/themes/standard/images/img.jpg" />

However on my pages I want to just do

<img src="https://server.com/images/img.jpg" />

However nothing seems to be working on my localhost. I am running the Apache server on a windows 7 machine. I am trying to use a .htaccess to do what I’ve mentioned above. Here is my .htaccess file in the root of my website.

UPDATE:
I tried ZweiBlumen suggestion below but that did not seem to work. I then tried Geerts suggestion and added the re-write method to my misc.php of my admin folder. I then went to my permalinks page and hit save. The result of doing this meant my .htaccess folder was rewritten and the output it produced is below.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^css/(.*) /wp-content/themes/standard/css/$1 [QSA,L]
RewriteRule ^js/(.*) /wp-content/themes/standard/js/$1 [QSA,L]
RewriteRule ^images/(.*) /wp-content/themes/standard/images/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

However, I still cannot navigate to my images folder such as:

http://localhost/images/myimage.jpg.

All I get is a page not found. Joshua’s suggestion worked perfectly however I’m hoping to use this in conjunction with masking the images URL.

Is there anything else I might be doing wrong, or should check?

UPDATE:

For anyone reading this, I just tried again and it has worked using a combination of Geerts and Joshuas methods. My Firefox browser appears to have been caching the page which was causing me to think it wasn’t.

The reason I went with this over putting it in the .htaccess file is that this file is overwritten every time I go to the permalinks Admin page and so I don’t won’t to overwrite this by accident. I guess I could turn this off somehow but not sure how to do that. All three answers helped to some degree.

3 s
3

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.

Leave a Reply

Your email address will not be published. Required fields are marked *