使用 mod_rewrite 在每个文件请求之前添加一个文件夹?

发布于 2024-10-01 13:53:29 字数 515 浏览 0 评论 0原文

我正在本地服务器和远程服务器上开发一个网站。在开发过程中,服务器位于 url.com/~user 中,而在开发中的本地主机上,我只是在 root 中进行开发。例如,如果我在本地服务器上包含一个文件,我只需引用“/img/img.png”...而这显然会在远程服务器上返回 404,因为它会寻找“/~user” /img/img.png”。

这与 @ 问题得到了解答指向本地服务器上的文件与远程服务器上的文件但是我想知道这是否可以用 mod_rewrite 来解决?定义 SITE_ROOT 在 php 文件中有效...但它不能修复 CSS 表中引用的文件。

是否可以简单地告诉我的远程服务器将 /~user/ 添加到每个单个文件包含中?即 /img/img.png 变成 /~user/img/img.png ?

感谢您的指点。

I'm developing a site on a local server and a remote server. While in development, the server has be in url.com/~user while on my localhost in development I am simply developing in root. ie if I include a file on the local server I simply refer to "/img/img.png" for example... while this is obviously going to return a 404 on the remote server as it would be looking for "/~user/img/img.png".

This is the same issue that was answered over @ Problems with pointing to files on local server versus remote however what I am wondering here is if this can be solved with mod_rewrite? Defining SITE_ROOT works within the php files... but it doesn't fix the files being referred to in the CSS sheets.

Is it possible to simply tell my remote server to add /~user/ to every single file inclusion? ie a /img/img.png becomes /~user/img/img.png ?

Thanks for any pointers.

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

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

发布评论

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

评论(1

追我者格杀勿论 2024-10-08 13:53:29

我猜测您正在生产中使用某种共享服务器。您的情况的问题在于 http://example.com/img/some.jpg 将生成一个请求(无需特殊设置)不会命中 http://example 中 Web 目录中的 .htaccess。 com/~用户/。因此,除非您有权访问服务器范围的 apache 配置,否则这对您不起作用。

如果您想走这条路线,您可以将 URL 写为 /~user/img/some.jpg。然后,您可以在开发服务器上进行重写,以摆脱 ~user。

更好的是,为什么要包含前导斜杠呢?这样,您就得到了 img/some.jpg,这将是一个相对路径。如果您在 http://localhost/index.html 中引用图像,它将拉取 http://localhost/img/some.jpg。如果您引用 http://example.com/~user/index.html,它将拉 http://example.com/~user/img/some.jpg< /a>.

最后,如果在开发服务器上有一个 ~user 目录,这样两个地方的目录都是一样的呢?

I'm going to take a guess that you are on some kind of shared server in production. The problem with your situation is that something like http://example.com/img/some.jpg will generate a request that (without special setup) will not hit .htaccess in the web directory in http://example.com/~user/. So, unless you have access to the server-wide apache config, this won't work for you.

What you could do if you want to go this route is write the URLs as /~user/img/some.jpg. You could do a rewrite on your development server, then, to get rid of the ~user.

Better yet, why include the leading slash at all? That way, you get just img/some.jpg, which will be a relative path then. If you are referencing the image in http://localhost/index.html, it will pull http://localhost/img/some.jpg. If you are referencing the image in http://example.com/~user/index.html, it will pull http://example.com/~user/img/some.jpg.

Finally, what about just having a ~user directory on your development server so it's the same in both places?

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