Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I added a .htaccess file to a wordpress website I'm working on, to show nice urls instead of page ids.

The content of the .htaccess is this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

It's working fine, but in every subpage I have it doesn't show images. Using Firebug I see that the path to the image is correct (for example: for the logo is the same path as in other pages), but it gives the error "Failed to load the given URL".

The src for the images is this: wp-content/themes/.../file.jpg. I noticed that putting an absolute url, such as http://mysite.com/wp-content/.../file.jpg does solve the problem.

Shall I just use absolute URLs or is there something I can do in the htaccess?

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

Your problem has nothing to do with htaccess. It's that you're using relative paths where you should be using absolute paths. (Here's an explanation of the difference)

Probably the best way to reference any assets in your theme (assuming your theme has an "images" subdirectory) is to do something like:

<?php echo get_bloginfo('template_url') ?>/images/your_image.jpg

The first part echoes the whole url, right up to your theme root. So you always get an absolute URL, and you don't have to hardcode the "/wp-content/themes/whatever". Some WP installs don't use "wp-content" as the name of the content directory, so this practice will make your themes more flexible and portable.

Update in response to comment:

This problem popped up because you enabled pretty permalinks, not because you changed your htaccess file. Granted, you did have to edit your htaccess enable pretty permalinks, but trust that this htaccess change was only incidental to the bigger issue.

Before you enabled pretty permalinks, all your page URIs were relative to the domain root. Meaning all your pages were "yourdomain.com/?p=something". In this situation, your relative image paths worked, because they were always relative to the root.

After you enabled pretty permalinks, everything was relative to whatever path you were currently on. So if you weren't on the homepage, any relative URL would be broken.

Take, just as an illustration, the relative url to an example image: "images/my_image.jpg". On the homepage ("www.yourdomain.com/"), your browser will look for this image at "www.yourdomain.com/images/my_image.jpg". But what happens if you're on an interior page like "www.yourdomain.com/about"? Your browser will look for this image at its relative path, which is "www.yourdomain.com/about/images/my_image.jpg". Obviously, this isn't the right path, and the image (rightly) failed to load.

share|improve this answer
    
Ok, maye it has nothing to do with htaccess directly and well and this solution works. Thank you. Anyway, I know the difference between an absolute path and a relative one. It's that this website has been made by someone else. The other day I just added the htaccess and now there's this problem. I'm really wondering what happened because I'm pretty sure they were working before and nobody did change them these days. –  Carlo May 25 '12 at 19:42
    
@Carlo Glad you got it working. I added more explanation to my answer to explain exactly what happened and why. –  MathSmath May 25 '12 at 20:21
    
ok, thanks for the detailed explanation. That's what I was looking for. I'd vote you up, but I can't. Anyway I can't use the php code inside a page. Am I wrong? –  Carlo May 26 '12 at 0:24
    
@Carlo Not sure what your question means. "I can't use the php code inside a page"? You can definitely use this php snippet in page templates. But if you're talking about using it in page content (content you entered through the WYSIWYG), then no, you can't. If you're using the "insert media" button on the WYSIWYG, it should insert the complete absolute path. If you're manually adding images, you can use relative URLs that start with a slash--this makes them always relative to the root (instead of being relative to your current path). –  MathSmath May 29 '12 at 14:40
add comment

Try making your image references start with /

They work on the homepage because plain wp-content is the correct path. But when you get deeper in, it needs to be /wp-content

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.