尝试使用 mod_rewrite 获取通用 php/javascript 库
这是对我之前发布的问题的新理解:
我有一个 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 的请求,这里是一个例子:
- 以 subdomain.example.com/test.html
- test.html 引用
- 在 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:
- Take for instance subdomain.example.com/test.html
- test.html references
<script src="/code/js/test.class.js" type="text/javascript" ></script>
- 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 ofsubdomain
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是
更改域(事实证明)更改重写内协议的副作用。通过在 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.