ajax - Apache "_escaped_fragment_"重定向

标签 ajax apache mod-rewrite seo

我在重写 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

为什么内部重定向不起作用?

更新
完整的 htaccess:https://www.dropbox.com/s/alqqjb11wy367sb/htaccess.txt
Apache 主要配置:https://www.dropbox.com/s/p23vv0dd5ffrfaj/conf.txt

最佳答案

似乎 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

通过在 Options 指令和 RewriteEngine 指令之间添加它来禁用 DirectoryIndex

DirectoryIndex disabled

在重定向下方和其他规则上方添加以下规则:

# Use index.html for requests to root without a proper query string
RewriteCond %{QUERY_STRING} !^_escaped_fragment_=$
RewriteRule ^$ /index.html [L]

关于ajax - Apache "_escaped_fragment_"重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25449487/

相关文章:

javascript - jQuery AJAX 获取 Vimeo 缩略图

javascript - 开发工具中的ajax状态是200,但是为什么会出错呢?

python - Flask(在 apache mod_wsgi 上)返回 BuildError

apache - 在 vhost 文件中使用符号链接(symbolic link)

.htaccess - htaccess 仅当 HTTP 引荐来源网址不等于某些内容时才重定向

ruby-on-rails - 如何在 AJAX 中将 RecordRTC blob 文件上传到 Rails 回形针

php - 将复选框值发送到 php 脚本以通过 jquery 进行处理

php - 执行我的 PHP 脚本时 Apache 崩溃了

apache - htaccess 使用 cookie 重定向多个域以记住所有内容

php - SEO 友好的 ModRewrite + 数据结构