PHP 使用 .htaccess 修复相对图像路径

发布于 2024-12-29 00:24:33 字数 793 浏览 1 评论 0原文

我有一个 php 文件,例如 www.example.com/folder/test.php

上面的文件 (test.php) 包含行

include($_SERVER['DOCUMENT_ROOT']. $page);

$page = /folder2/.$directory1./.$directory2./.index.php

$directory1$directory2 都是随机的。

但是,

索引页上有一个图像,仅引用该图像作为源。

例如 src="image.bmp"

那么图像的 url 实际上会像这样显示...... www.example.com/folder/image.bmp

它应该像这样显示...... www.example.com/folder2/.$directory1./.$directory2./image.bmp

除了使用绝对路径之外,还有其他方法可以解决此问题吗? 有数百万个目录,我们希望纠正问题而不是纠正问题(即使我们会同时做这两件事)。

我可以在查找图像的目录中放置一个 htaccess 文件来重定向它吗?

发件人:www.example.com/folder/

至:www.example.com/folder2/.$directory1./.$directory2./

I have a php file, for example www.example.com/folder/test.php

The file above (test.php) has the line

include($_SERVER['DOCUMENT_ROOT'].$page);

$page = /folder2/.$directory1./.$directory2./.index.php

$directory1 and $directory2 are all random.

However,

The index page has an image on it that just references the image as the source.

For Example src="image.bmp"

So what happens is the image's url actually shows up like this...
www.example.com/folder/image.bmp

And it should show up like this...
www.example.com/folder2/.$directory1./.$directory2./image.bmp

Is there a way to fix this besides using absolute paths?
There are millions of directories and we would like to correct the issue rather than correct the problem (even though we will do both).

Can I place an htaccess file in the directory its looking for the image in to redirect it?

FROM: www.example.com/folder/

TO: www.example.com/folder2/.$directory1./.$directory2./

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

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

发布评论

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

评论(1

酒绊 2025-01-05 00:24:33

如果您想这样做,您应该动态更改 .htaccess 或在其中指定任何可能的 $directory1/$directory2 组合。在我看来,最好的方法是将文件 index.php 中该图像的 src 更改为以下内容:

<img src="/folder2/<?php echo $directory1; ?>/<?php echo $directory2; ?>" />

If you want to do that, you should either dynamically change the .htaccess or specify in it any possible $directory1/$directory2 combination. In my opinion, the best way to do this is to change the src of that image in the file index.php to something like:

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