0

I am trying to get rewriting working in an .htaccess file even when a file in the same location as the requested URL exists.

i.e. I want a rewrite of /about/index.html to happen even if there is a file at /about/index.html .

Currently I have:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^thehost.example.com$
RewriteCond %{REQUEST_URI} !^/dont-rewrite-when-at-this-url
RewriteRule ^(.*)(/|\.html|\.txt)$ /path/to/stubfile.html?/$1$2 [last,qsappend]

The problem is that if I request

http://thehost.example.com/about/index.html

and a file at /about/index.html exists (which is what I want, i.e. the rewrite should happen if the file DOES exist, though it would be fine for it also to happen if it didn't)

then I get error 500: infinite recursion.

(Adding a RewriteCond to exempt the stubfile itself being rewritten makes no difference.)

htaccess rewrite causes 500 error instead of 404 suggests that the absence of a -f RewriteCond prevents the recursion, but then that results in the behaviour I want not happening.

Is there a way to get the Rewrite to happen, i.e. ignore the fact that a file is there and just rewrite anyway even if there is a file of the same URL present?

Community
  • 1
  • 1
fooquency
  • 1,575
  • 3
  • 16
  • 29
  • Just to clarify - this is an internal infinite recursion error (resulting in a visible 500 error in the browser) - not a Redirect Loop (which would be reported as such by the browser). – fooquency Dec 13 '09 at 23:51

1 Answers1

0

Just exclude the destination you are redirecting to, in this case /path/to/stubfile.html:

RewriteCond %{HTTP_HOST} ^thehost.example.com$
RewriteCond %{REQUEST_URI} !^/dont-rewrite-when-at-this-url
RewriteCond %{REQUEST_URI} !=/path/to/stubfile.html
RewriteRule ^(.*)(/|\.html|\.txt)$ /path/to/stubfile.html?/$1$2 [last,qsappend]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Nope; as I said, adding a RewriteCond to exempt the stubfile itself still results in the recursion. Thanks for the suggestion anyway. – fooquency Dec 13 '09 at 23:44
  • @fooquency: What is `/path/to/stubfile.html`? A URL path or a filesystem path? – Gumbo Dec 13 '09 at 23:59