PHP:mod_rewrite 应用程序中链接中断的最佳解决方案

发布于 2024-08-26 13:59:17 字数 956 浏览 4 评论 0原文

我正在使用 mod rewrite 将所有针对不存在的文件/目录的请求重定向到 index.php?url=*

这肯定是您使用 mod_rewrite 执行的最常见的操作,但我有一个问题:

自然,如果页面url是“mydomain.com/blog/view/1”,浏览器将在“虚拟”目录“mydomain.com/blog/view/”中查找图像、样式表和相关链接。

问题 1:

  • 使用基本标签是最佳解决方案吗?不过,我发现没有一个 PHP 框架使用基本标签。

  • 我目前正在使用正则表达式替换所有相对链接,以在输出之前指向正确的路径。这“可以”吗?

问题2:

服务器可能不支持mod_rewrite。但是,所有公共文件(例如图像、样式表和请求收集器 index.php)都位于目录 /myapp/public 中。通常 mod_rewrite 将所有请求指向 /public 所以看起来 public 实际上也是所有用户的根目录。

但是,如果没有 mod_rewrite,我必须使用 header() 调用将用户从根目录指向 /public。然而,这意味着所有链接都会再次损坏,因为突然间所有图像等都必须通过 /public/myimage.jpg 调用

附加信息:当没有 mod_rewrite 时,上述请求将如下所示:mydomain.com/public/index.php/blog/view/1

  • 这两个问题的最佳解决方案是什么?

编辑/附加问题:

有没有办法使用纯 htaccess 代码创建 /public/ 基本目录?

I'm using mod rewrite to redirect all requests targeting non-existent files/directories to index.php?url=*

This is surely the most common thing you do with mod_rewrite yet I have a problem:

Naturally, if the page url is "mydomain.com/blog/view/1", the browser will look for images, stylesheets and relative links in the "virtual" directory "mydomain.com/blog/view/".

Problem 1:

  • Is using the base tag the best solution? I see that none of the PHP frameworks out there use the base tag, though.

  • I'm currently having a regex replace all the relative links to point to the right path before output. Is that "okay"?

Problem 2:

It is possible that the server doesn't support mod_rewrite. However, all public files like images, stylesheets and the requests collector index.php are located in the directory /myapp/public. Normally mod_rewrite points all request to /public so it seems as if public was actually the root directory too all users.

However if there is no mod_rewrite, I then have to point the users to /public from the root directory with a header() call. That means, however that all links are broken again because suddenly all images, etc. have to be called via /public/myimage.jpg

Additional info: When there is no mod_rewrite the above request would look like this: mydomain.com/public/index.php/blog/view/1

  • What would be the best solutions for both problems?

Edit/Additional question:

Is there a way to make /public/ the base dir using plain htaccess code?

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

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

发布评论

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

评论(3

§对你不离不弃 2024-09-02 13:59:17

以不需要 mod_rewrite 来运行的方式编写应用程序(以具有“丑陋”的 url 为代价)。使用 mod_rewrite 逐步增强它以达到预期的结果。这可能意味着您需要在应用程序中存储一些基本路径配置信息。

Write the app in such a way that it doesn't need mod_rewrite to function (at the cost of having "ugly" urls). Progressively enhance it with mod_rewrite to achieve the desired result. This probably means that you'll need to store some base path config info in your app.

梦情居士 2024-09-02 13:59:17

我根本不明白这些问题。
是的,这肯定是您使用 mod_rewrite 执行的最常见的操作,有两个条件:

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

因此,不会损害您现有的图像。

为什么不只使用绝对路径,例如/myapp/public/myimage.jpg,那么没有虚拟目录会损害图像路径?

I don't understand these problems at all.
Yes, this is surely the most common thing you do with mod_rewrite, yet with 2 conditions:

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

So, nothing hurt your existing images.

Why not to use just absolute path, e.g. /myapp/public/myimage.jpg, so, no virtual directory will hurt image path?

面犯桃花 2024-09-02 13:59:17

路径信息怎么样?您可以在没有 mod_rewrite

/index.php/path/to/another/file.jpg

<?php
echo $_SERVER["PATH_INFO"]; // outputs /path/to/another/file.jpg
?>

的情况下使用它无论如何,如果您想知道您的服务器是否支持 mod_rewrite :

<?php
echo "mod_rewrite : ".(!empty($_SERVER["REDIRECT_URL"])?"supported":"not supported");
?>

那么您就会知道 mod_rewrite 是否是解决方案,或者也许 path_info 更好适合您,您可以创建也可以查找两者的支持功能。

what about path info? You could use it without mod_rewrite

/index.php/path/to/another/file.jpg

<?php
echo $_SERVER["PATH_INFO"]; // outputs /path/to/another/file.jpg
?>

Anyways, if you want to know if mod_rewrite is supported by your server :

<?php
echo "mod_rewrite : ".(!empty($_SERVER["REDIRECT_URL"])?"supported":"not supported");
?>

Then you ll know if mod_rewrite is the solution or maybe path_info is more well suited for you, you could make support functions that could look for both too.

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