.htaccess将带有图像的URL从文件夹重写到php文件

编程入门 行业动态 更新时间:2024-10-21 23:29:32
本文介绍了.htaccess将带有图像的URL从文件夹重写到php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将wp-content/uploads文件夹中的所有图像重写为wp-content/thumb文件夹中的thumbs.php文件,而无需更改url

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

这是我在.htacccess中添加的内容

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=mywebsite/$1.$2 [L] </IfModule> # END img # other wordpress default stuff including wp-fastest-cache

这是我想要实现的 访客访问我的网站或任何带有url之类的图片

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

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

并且重写应该是

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

*对于那些可能认为... thumbs.php不是timthumb文件的人.我已经编写了自己的脚本来实现无损图像压缩

*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?

最终更新: 对于以后找到答案的人. 所以最后是因为我的WP FASTEST CACHE htaccess代码.刚刚删除了-d指令,并像一个符咒一样工作.但是我不再使用此代码,因为我找到了更好的选项.

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.

代码如下:

# 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=mywebsite/$1.$2 [L] </IfModule> # END img # other wordpress default stuff including wp-fastest-cache

推荐答案

RewriteCond %{REQUEST_FILENAME} -f RewriteCond %{REQUEST_FILENAME} -d

所请求的映像永远不会既是文件又是目录,因此您的条件将永远不会匹配,并且请求也将不会被重写.对图像的有效请求应仅是文件,因此请删除第二个RewriteCond指令(-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)?

更多推荐

.htaccess将带有图像的URL从文件夹重写到php文件

本文发布于:2023-11-01 20:15:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:写到   文件夹   图像   文件   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!