我正在尝试将所有传入的URL(不链接到文件系统上的物理文件)重写到我的index.html(并进一步处理带有history.js的URL)
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# Rewrite everything that isn't a real file to index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ www.xy.de/index.html
localhost/www.xy.de/<foo>
效果很好,但
localhost/www.xy.de/<foo>/<bar>
不是真的。即使在第二个例子中,它也会被重定向到index.html,但会发生一些奇怪的事情:
http://sprunge.us/GiEM(以下同样为了更好的阅读)
[...][rid#1dfc7e0/initial] (3) [perdir /srv/http/www.xy.de/] add path info postfix: /srv/http/www.xy.de/yxc -> /srv/http/www.xy.de/yxc/z
[...][rid#1dfc7e0/initial] (3) [perdir /srv/http/www.xy.de/] strip per-dir prefix: /srv/http/www.xy.de/yxc/z -> yxc/z
[...][rid#1dfc7e0/initial] (3) [perdir /srv/http/www.xy.de/] applying pattern '^(.+)$' to uri 'yxc/z'
[...][rid#1dfc7e0/initial] (4) [perdir /srv/http/www.xy.de/] RewriteCond: input='/srv/http/www.xy.de/yxc' pattern='!-f' => matched
[...][rid#1dfc7e0/initial] (4) [perdir /srv/http/www.xy.de/] RewriteCond: input='/srv/http/www.xy.de/yxc' pattern='!-d' => matched
[...][rid#1dfc7e0/initial] (2) [perdir /srv/http/www.xy.de/] rewrite 'yxc/z' -> 'www.xy.de/index.html'
[...][rid#1dfc7e0/initial] (3) [perdir /srv/http/www.xy.de/] add per-dir prefix: www.xy.de/index.html -> /srv/http/www.xy.de/www.xy.de/index.html
[...][rid#1dfc7e0/initial] (2) [perdir /srv/http/www.xy.de/] trying to replace prefix /srv/http/www.xy.de/ with /
[...][rid#1dfc7e0/initial] (5) strip matching prefix: /srv/http/www.xy.de/www.xy.de/index.html -> www.xy.de/index.html
[...][rid#1dfc7e0/initial] (4) add subst prefix: www.xy.de/index.html -> /www.xy.de/index.html
[...][rid#1dfc7e0/initial] (1) [perdir /srv/http/www.xy.de/] internal redirect with /www.xy.de/index.html [INTERNAL REDIRECT]
[...][rid#1e08850/initial/redir#1] (3) [perdir /srv/http/www.xy.de/] strip per-dir prefix: /srv/http/www.xy.de/index.html -> index.html
[...][rid#1e08850/initial/redir#1] (3) [perdir /srv/http/www.xy.de/] applying pattern '^(.+)$' to uri 'index.html'
[...][rid#1e08850/initial/redir#1] (4) [perdir /srv/http/www.xy.de/] RewriteCond: input='/srv/http/www.xy.de/index.html' pattern='!-f' => not-matched
[...][rid#1e08850/initial/redir#1] (1) [perdir /srv/http/www.xy.de/] pass through /srv/http/www.xy.de/index.html
[...][rid#1e00800/initial] (3) [perdir /srv/http/www.xy.de/] add path info postfix: /srv/http/www.xy.de/yxc -> /srv/http/www.xy.de/yxc/static/main.css
[...][rid#1e00800/initial] (3) [perdir /srv/http/www.xy.de/] strip per-dir prefix: /srv/http/www.xy.de/yxc/static/main.css -> yxc/static/main.css
[...][rid#1e00800/initial] (3) [perdir /srv/http/www.xy.de/] applying pattern '^(.+)$' to uri 'yxc/static/main.css'
[...][rid#1e00800/initial] (4) [perdir /srv/http/www.xy.de/] RewriteCond: input='/srv/http/www.xy.de/yxc' pattern='!-f' => matched
[...][rid#1e00800/initial] (4) [perdir /srv/http/www.xy.de/] RewriteCond: input='/srv/http/www.xy.de/yxc' pattern='!-d' => matched
[...][rid#1e00800/initial] (2) [perdir /srv/http/www.xy.de/] rewrite 'yxc/static/main.css' -> 'www.xy.de/index.html'
[...][rid#1e00800/initial] (3) [perdir /srv/http/www.xy.de/] add per-dir prefix: www.xy.de/index.html -> /srv/http/www.xy.de/www.xy.de/index.html
[...][rid#1e00800/initial] (2) [perdir /srv/http/www.xy.de/] trying to replace prefix /srv/http/www.xy.de/ with /
[...][rid#1e00800/initial] (5) strip matching prefix: /srv/http/www.xy.de/www.xy.de/index.html -> www.xy.de/index.html
[...][rid#1e00800/initial] (4) add subst prefix: www.xy.de/index.html -> /www.xy.de/index.html
[...][rid#1e00800/initial] (1) [perdir /srv/http/www.xy.de/] internal redirect with /www.xy.de/index.html [INTERNAL REDIRECT]
问题出在这一行:
add path info postfix: /srv/http/www.xy.de/yxc -> /srv/http/www.xy.de/yxc/static/main.css
对于html文件中的所有图像/脚本/样式表(所有相关链接)都会发生这种情况,当然因为没有带有此前缀的文件,所有内容都会被重定向到index.html并且一切都搞砸了。
为什么网络服务器会将yxc
添加到路径中?我该如何防止这种行为?
答案 0 :(得分:1)
为什么网络服务器会将yxc添加到路径中?我该如何防止这种行为?
这是相对链接的工作方式。浏览器尝试使用请求的URI确定URI基础是什么。因此,当浏览器请求http://localhost/ww.xy.de/yxc/z
时,URI为/www.xy.de/yxc/z
,BASE为/www.xy.de/yxc/
。有了这个基础,所有相关链接都会将其附加到链接的前面。
您需要将所有链接更改为绝对链接,或添加
<base href="/www.xy.de/">
到index.html页面的标题。