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'm trying to add some rewriterules in the .htaccess file to deal with hotlinked images - specifically I want to add the URL from the post where the image appeared to the header location information of the hotlinked image request.

I've got working htaccess & PHP code to add the header info, but in order to make it work, I need to be able to somehow look up the URL for the post from the image URL (this blog has a unique post for each image). I've got working code to do this, but it uses the Wordpress API, and when I try to call it none of the functions are recognized.

I'm thinking that maybe calling the API from htaccess isn't possible... like it's out of scope, or I'm calling from a place that can't see Wordpress or something? I'm wondering if there's a way to make Wordpress API calls accessible from htaccess - or if anybody has any suggestions for a workaround.

The only thing I can think of would be to try to query the database directly through MySQL, but I'm a bit nervous about doing that, and I'm not quite sure I understand the data structure fully.

Any suggestions would be welcomed!

share|improve this question
    
Try using the Rewrite API instead of .htaccess. Here's a great tutorial –  bungeshea Feb 6 '13 at 11:06
    
You can also try including the wp-load.php file from the root of your WordPress installation to access the WordPress API –  bungeshea Feb 6 '13 at 11:07

2 Answers 2

To access your WordPress functions within a standalone PHP file, add the two following lines at the beginning of your PHP file:

define( 'WP_USE_THEMES', false );
require('wp-blog-header.php'); # or modify to match your current wp-blog-header.php file  
share|improve this answer
    
This worked! Thank You! –  Catia Feb 6 '13 at 7:35

The .htaccess is parsed before WordPress is loaded, so you don’t get access to WordPress data.

You can make the redirect in WordPress, in a plugin: Hook into 404_template and inspect $_SERVER['REQUEST_URI'] with parse_url(). You should be able to find the matching post for the image then.
You did not write how the permalinks for these posts are built, so we cannot offer a more specific solution.

share|improve this answer
    
Thanks for your response... I fear I am a tad bit too inept to be able to implement it at this point (total newbie to WP programming) so I went with rrikesh's down and dirty solution. But thanks - maybe someday I'll be accomplished enough to get there! :-) –  Catia Feb 6 '13 at 7:37

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.