Yury Bushev
Yury Bushev

Reputation: 692

Apache "_escaped_fragment_" redirection

I have a trouble with Apache rewrite, for some reasons the first rule above NOT wokring WITHOUT flag "R".

But I want make redirection internaly.

# 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 log:

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

Why INTERNAL REDIRECT does not work?

UPDATE
Full htaccess: https://www.dropbox.com/s/alqqjb11wy367sb/htaccess.txt
Apache main config: https://www.dropbox.com/s/p23vv0dd5ffrfaj/conf.txt

Upvotes: 1

Views: 1178

Answers (1)

Sumurai8
Sumurai8

Reputation: 20737

It seems that the DirectoryIndex directive takes precedence over what you do with mod_rewrite (or probably better said: It seems that mod_dir completely ignores what mod_rewrite does with the url). Apache will go through .htaccess repeatedly until the url stays stable. What you see it that on the second invocation, after the line with [INTERNAL REDIRECT] it starts of with a different url. This is the url mod_dir assigned, not the url mod_rewrite assigned. If you do a redirect this is not a problem. All activity is stopped when you signal Apache that a redirect must happen and you stop applying rules with the [L] directive. Apache will then immedeatelly send the redirect back to the client, which will request this new url from the server.

The easiest solution seems to add a rule for what DirectoryIndex normally would do, then disable DirectoryIndex. I would also add Options -Indexes to prevent mod_index from spilling the contents of folders if no file in it is requested.

Add the following Option to the top of your .htaccess:

Options -Indexes

Disable DirectoryIndex by adding this between the Options directive and the RewriteEngine directive.

DirectoryIndex disabled

Add the following rule below your redirect and above your other rules:

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

Upvotes: 2

Related Questions