Htaccess 将一张图片替换为另一张图片

发布于 2025-01-14 22:52:50 字数 787 浏览 1 评论 0原文

所以我一直在编写一个代码,在不更改链接的情况下用另一个图像替换一个图像。

这是我在其中一个论坛上找到的代码。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]
</IfModule>

因此,此代码不仅将用户重定向到另一个页面,而且还重定向到随机链接。 所以原始链接是 http://toss.rf.gd/storage/fredShip1.png 虽然它应该用http://toss.rf.gd/storage/fredShip2.png替换图像(只是一个示例),但它会将用户发送到此处 toss.rf.gd/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png

我也添加了图像 -> 图片

我真的不擅长 htaccess,所以如果我错了,请务必纠正我。另外,英语不是我的母语,所以预计会出现一些小错误。

编辑:所以我通过重定向到随机链接解决了问题。但我仍然想知道是否可以只更改图像而不更改链接?

So Ive been making a code which replaces one image with another without changing the link.

So heres the code that I found on one of the forums.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]
</IfModule>

So this code not only redirects user to another page but also to a random link.
So original link is http://toss.rf.gd/storage/fredShip1.png though it should have replaced the image with http://toss.rf.gd/storage/fredShip2.png(Just an example) but it sends the user here toss.rf.gd/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png

I added the image too ->
The image

I am really bad at htaccess so make sure correct me if Im wrong. Also english is not my first language so expect some minor mistakes.

EDIT : So i solved the problem with redirection to a random link. But Im still wondering is it possible to just change the image without changing the link?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2025-01-21 22:52:50
RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]

您发布的代码基本上已经满足您的要求,只是您需要调整路径以匹配您的示例。上述规则的“问题”在于它将请求重写为 /fredShip2.png (在文档根目录中),而不是您的 /storage/fredShip2.png例子。

假设 .htaccess 文件位于网站的文档根目录中,并且您希望在内部将请求从 /storage/fredShip1.png 重写为 /storage/fredShip2 .png 那么你可以这样做:

RewriteRule ^storage/fredShip1.png$ storage/fredShip2.png [L]

任一参数的 URL 路径上都不应该有斜杠前缀。

如果您的 .htaccess 文件中有其他指令,那么这些指令的顺序可能很重要。

确保在测试之前已清除浏览器缓存。


但它会将用户发送到此处example.com/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png

您发布的指令是不可能的。这很可能是由于早期(错误)301(永久)重定向实验而导致的缓存重定向。例如,类似以下内容将产生上述“错误”输出:

RewriteRule fredShip1\.png$ storage/FredShip2.png [R=302,L]

请注意使用 R (redirect) 标志,并且 R 上缺少斜杠前缀。 code>RewriteRule 替换 字符串(第二个参数)。由于替换字符串是“相对”的,因此目录前缀(即示例中的/home/vol8_1/[Account info]/htdocs/)被添加到替换之前由于这是一个外部重定向(如 R 标志所示),因此会向用户公开绝对文件系统路径。

注意:上面是 302(临时)重定向 - 因此浏览器不应该缓存(至少默认情况下不缓存)。

RewriteRule ^.*/fredShip1.png$ /fredShip2.png [L]

The code you've posted already does essentially what you require, except that you need to adjust the paths to match your example. The "problem" with the above rule is that it rewrites the request to /fredShip2.png (in the document root), not /storage/fredShip2.png as in your example.

Assuming the .htaccess file is in the document root of the site and you wish to internally rewrite the request from /storage/fredShip1.png to /storage/fredShip2.png then you would do it like this:

RewriteRule ^storage/fredShip1.png$ storage/fredShip2.png [L]

There should be no slash prefix on the URL-path in either argument.

If you have other directives in your .htaccess file then the order of these directives can be important.

Make sure you've cleared your browser cache before testing.


but it sends the user here example.com/home/vol8_1/[Account info]/htdocs/storage/FredShip2.png

That's not possible with the directive you've posted. This is most likely a cached redirect due to an earlier (erroneous) experiment with 301 (permanent) redirects. For example, something like the following would produce the above "erroneous" output:

RewriteRule fredShip1\.png$ storage/FredShip2.png [R=302,L]

Note the use of the R (redirect) flag and the lack of a slash prefix on the RewriteRule substitution string (2nd argument). Since the substitution string is "relative", the directory-prefix (ie. /home/vol8_1/[Account info]/htdocs/ in your example) is prepended to substitution and since this is an external redirect (as denoted by the R flag) this then exposes the absolute filesystem path to the user.

NB: The above is a 302 (temporary) redirect - so should not be cached by the browser (at least not by default).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文