深mod_rewrite的网址为index.html的休息时间相对路径 [英] mod_rewrite to index.html breaks relative paths for deep URLs
问题描述
有关单页的应用程序,我有以下的重写规则在我的.htaccess文件的所有流量引导到的index.html,这样一个JS可以相应地解析URL和消防控制器。
For a single-page app, I have the following RewriteRule in my .htaccess file to direct all traffic to index.html so that a JS can parse the URL and fire controllers accordingly.
# html5 pushstate (history) support:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
这工作正常顶级的网址一样,www.mydomain.com/resource但更深层次的东西,像www.mydomain.com/resource/123,打破了当前目录('。'),而指数值。 HTML
This works fine for top level urls like, www.mydomain.com/resource but anything deeper, like www.mydomain.com/resource/123, breaks the value of the current directory ('.') while in index.html.
例如,在我的index.html像这样的脚本标签
For example, a script tag in my index.html like this
<script src="js/app/config.js"></script>
将转化为SRC =资源/应用/ config.js
would translate into src="resource/app/config.js"
或者,对于像'www.mydomain.com/more/nested/resource/123在那同样的js文件在src将PTED为多/嵌套/资源/应用/配置间$ P $的URL。 JS。
Or, for a url like 'www.mydomain.com/more/nested/resource/123' the src on that same js file would be interpreted as "more/nested/resource/app/config.js".
不用说,这些文件不存在,并且该应用场所
Needless to say, those files don't exist and the app breaks.
任何人都可以摆脱任何光线是怎么回事呢?谢谢你。
Can anybody shed any light to what is going on here? Thanks.
推荐答案
我得到它的工作,这是多么的:
I got it to work and this is how:
当你给下面的SRC:
<script src="js/app/config.js"></script>
您说得对,阿帕奇正确重新路由到的index.html(或者你有什么后备网址),然后尝试比较根据在URL中嵌套路径来访问资源。
you're right in that Apache correctly reroutes to index.html (or whatever fallback URL you have) and then tries to access resources relatively according to nested path in the URL.
要解决这个问题,你需要有一个领先的/来表示的根目录,如下所示:
To correct this, you need to have a leading "/" to signify the root directory, as follows:
<script src="/js/app/config.js"></script>
有一次,我这样做是为了我的JS库,CSS床单等,它的工作完美的罚款和处理Backbone.js的所有URL从那里出来。
Once I did this for my js libraries, css sheets, etc, it worked perfectly fine and backbone.js handled all URLs from there out.
注意:在Apache中2.2+,你现在可以使用的 FallbackResource 而不是mod_rewrite的,它需要较少的线路和完成同样的事情。
Note: In Apache 2.2+, you can now use FallbackResource instead of mod_rewrite, it requires fewer lines and accomplishes the same thing.
推荐的另一件事是使用绝对URL时,您可以。
Another thing recommended is to use absolute URLs whenever you can.
这篇关于深mod_rewrite的网址为index.html的休息时间相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!