【问题标题】:Apache "_escaped_fragment_" redirectionApache“_escaped_fragment_”重定向
【发布时间】:2014-10-16 10:53:12
【问题描述】:

我在 Apache 重写时遇到问题,由于某些原因,上面的第一条规则 NOT wokring WITHOUT flag "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

【问题讨论】:

    标签: ajax apache mod-rewrite seo


    【解决方案1】:

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

    【讨论】:

    • 太棒了!建议的解决方案对我来说没问题!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多