简体   繁体   中英

.htaccess Rewrite url with images from a folder to a php file

I want to rewrite all the images in my wp-content/uploads folder to a thumbs.php file in wp-content/thumb folder without changing url

This is what I have added in my .htacccess

# BEGIN img
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_USER_AGENT} !someweirdcrawler
RewriteRule ^(wp-content/uploads/.+?)\.(jpe?g|png|gif|svg)$ /wp-content/thumbs/index.php?img_source=http://mywebsite.com/$1.$2 [L]
</IfModule>
# END img
# other wordpress default stuff including wp-fastest-cache

This is what I want to achieve Visitors visit my site or any image with urls like

http://mywebsite.com/wp-content/uploads/2050/12/Hello-Dolly-1.0.0.1-words.jpg

and the rewrites should be

http://mywebsite.com/wp-content/thumbs/index.php?img_source=http://mywebsite.com/wp-content/uploads/2050/12/Hello-Dolly-1.0.0.1-words.jpg

*For those who may think... thumbs.php is not timthumb file. I have written my own script for lossless image compression

UPDATE: Its working for my other site.. What could be the problem?

FINAL UPDATE: For Those Who come to find answers Later. So Finally It was because of my WP FASTEST CACHE htaccess Code. Just removed the -d Directive and worked like a charm. But I am not using this code anymore because I have found better Options.

Here's the code:

# BEGIN img
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
#show original images to Image crawler Like wordpress photon or google images.
RewriteCond %{HTTP_USER_AGENT} !someweirdcrawler
RewriteRule ^(wp-content/uploads/.+?)\.(jpe?g|png|gif|svg)$ /wp-content/thumbs/index.php?img_source=http://mywebsite.com/$1.$2 [L]
</IfModule>
# END img
# other wordpress default stuff including wp-fastest-cache
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d

The requested image is never going to be both a file and a directory, so your condition is never going to match and the request is never going to be rewritten. A valid request for an image should only be a file, so remove the second RewriteCond directive ( -d directory check).

Its working for my other site..

Are you sure (the code is the same)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM