尝试使用 mod_rewrite 获取通用 php/javascript 库

发布于 2024-10-01 11:16:47 字数 1245 浏览 0 评论 0原文

这是对我之前发布的问题的新理解:

我有一个 mod_rewrite 片段,用于查找 javascript、css、php 文件是否存在于调用它们的子域上(例如 subdomain.example.com )。如果它们不存在,那么 apache 会“重定向”到常见 js、css 和 php 文件所在的 common.example.com。参见下文:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(js|css|php)$ http://common.example.com/$1.%2 [L]

如果满足以下条件,此方法有效:

  • 您将请求的文件名显式插入地址栏中。
  • 如果 js 或 css 在 html 标记中被引用/链接,它也可以工作。

则它不起作用

  • 如果url 在 php 或 js 文件中使用(例如,用于 ajax 调用), 。相反,它引用了找到 html 页面的子域(即 mod_rewrite 没有捕获它): subdomain.example.com

感谢您的帮助

编辑

根据 Pekka 的请求,这里是一个例子:

  1. subdomain.example.com/test.html
  2. test.html 引用
  3. test.class.js 中有 $.ajax({ url: " /code/php/test.php", 数据类型 : "json", data: {cmd : 'someCommand'}, success: callback()});

..

  • 所以在 #2 中,它找到 common.example.com/code/js/test .class.js (正确!)
  • 但是在 #3 中,ajax 调用 subdomain.example.com/code/php/test.php (不正确!因为 subdomain )

This is a new understanding of a question I posted previously:

I have a mod_rewrite snippet that finds out whether javascript, css, php files exist on the subdomain where they are called (e.g., subdomain.example.com). If they don't exist, then apache "redirects" to common.example.com where common js, css, and php files are. See below:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(js|css|php)$ http://common.example.com/$1.%2 [L]

This works if:

  • You explicitly plug the requested file name into the address bar.
  • It also works if the js or css is referenced/linked in the html markup.

It does not work if

  • The url is used inside a php or js file (e.g., for an ajax call). Instead it references the subdomain where the html page is found (i.e., mod_rewrite doesn't catch it): subdomain.example.com

Thanks SO for your help

EDIT

Per Pekka's requestion, here is an example:

  1. Take for instance subdomain.example.com/test.html
  2. test.html references <script src="/code/js/test.class.js" type="text/javascript" ></script>
  3. In test.class.js there is $.ajax({ url: "/code/php/test.php", dataType
    : "json", data: {cmd : 'someCommand'}, success: callback()});

..

  • So in #2, it finds common.example.com/code/js/test.class.js (Correct!)
  • But in #3, the ajax calls on subdomain.example.com/code/php/test.php (Incorrect! because of subdomain)

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

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

发布评论

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

评论(1

绝情姑娘 2024-10-08 11:16:47

这是更改域(事实证明)更改重写内协议的副作用。

通过在 RewriteRule 中指定完整的 URL 而不是文件系统路径,您可以强制服务器为每个请求发送 301 重定向标头。

AJAX 请求通常遵循标头重定向,< em>但是您正在重定向到不同的域,从而违反了同源政策

这意味着对于Ajax请求,不能使用这种重定向方法。您必须在 RewriteRule 中指定文件系统路径,以便重写可以在内部进行(这样对于浏览器来说,它仍然是 subdomain.example.com/code/php/test.php)。我不知道这是否可以在您的设置中完成。

This is a side-effect of changing thedomain (as it turned out) changing the protocol inside the rewrite.

By specifying a full URL in your RewriteRule instead of a filesystem path, you force the server to send a 301 redirect header for every request.

AJAX requests usually follow header redirects, but you are redirecting to a different domain, thus violating the Same Origin Policy!

This means that for Ajax requests, you can't use this redirection method. You would have to specify a filesystem path in your RewriteRule so the rewrite can take place internally (so that to the browser, it stays subdomain.example.com/code/php/test.php). I don't know whether this can be done in your setup.

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