我在Apache重写方面遇到了麻烦,由于某些原因,上面的第一条规则没有标记为“ R ”。
但我希望在内部进行重定向。
# FROM http://mysite.ru
# TO http://mysite.ru/?_escaped_fragment_=
# SNAPSHOT /mysite/server/base/public/snapshots/index.html
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=$
RewriteRule ^$ /snapshots/index.html? [NC,R,L] # NOK (if R missed)
# FROM http://mysite.ru/object/767
# TO http://mysite.ru/object/767?_escaped_fragment_=
# SNAPSHOT /mysite/server/base/public/snapshots/object/767.html
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ /snapshots%{REQUEST_URI}%1.html? [NC,L] # OK
Apache日志:
init rewrite engine with requested uri /
pass through /
strip per-dir prefix: /mysite/server/base/public/ ->
applying pattern '^(.*)$' to uri ''
RewriteCond: input='mysite.ru' pattern='^www\\.(.*)$' [NC] => not-matched
strip per-dir prefix: /mysite/server/base/public/ ->
applying pattern '^$' to uri ''
RewriteCond: input='_escaped_fragment_=' pattern='^_escaped_fragment_=$' => matched
rewrite '' -> '/snapshots/index.html?'
split uri=/snapshots/index.html? -> uri=/snapshots/index.html, args=<none>
internal redirect with /snapshots/index.html [INTERNAL REDIRECT]
init rewrite engine with requested uri /index.html
pass through /index.html
strip per-dir prefix: /mysite/server/base/public/index.html -> index.html
为什么INTERNAL REDIRECT不起作用?
更新
完整的htaccess:https://www.dropbox.com/s/alqqjb11wy367sb/htaccess.txt
Apache主配置:https://www.dropbox.com/s/p23vv0dd5ffrfaj/conf.txt
答案 0 :(得分:2)
似乎DirectoryIndex
指令优先于你对mod_rewrite的处理(或者更好的说法:似乎mod_dir完全忽略了mod_rewrite对url的作用)。 Apache将重复执行.htaccess,直到url保持稳定。您在第二次调用时看到的是,在[INTERNAL REDIRECT]
行之后,它以不同的URL开头。这是分配的url mod_dir,而不是分配的url mod_rewrite。如果你进行重定向,这不是问题。当您向Apache发出必须发生重定向并且您停止使用[L]
指令应用规则时,所有活动都会停止。然后Apache会立即将重定向发送回客户端,客户端将从服务器请求这个新URL。
最简单的解决方案似乎为DirectoryIndex
通常会执行的操作添加规则,然后禁用DirectoryIndex
。我还要添加Options -Indexes
以防止mod_index在文件夹内容没有被请求的情况下溢出文件夹的内容。
将以下选项添加到.htaccess的顶部:
Options -Indexes
通过在DirectoryIndex
指令和Options
指令之间添加此项来禁用RewriteEngine
。
DirectoryIndex disabled
在重定向下方和其他规则之上添加以下规则:
# Use index.html for requests to root without a proper query string
RewriteCond %{QUERY_STRING} !^_escaped_fragment_=$
RewriteRule ^$ /index.html [L]