Search code examples
url-rewritingisapi-rewrite

(ISAPI_Rewrite) Rewrite domain when certain file is requested


I am trying to create a condition to rewrite a url from http://subdomain.example.com/foo/foofile.txt to http://example.com/foo/foofile.txt when the request has foo/foofile.txt in it. If someone could point me in the right direction, I would appreciate any and all help...

Thanks in advance, B


Solution

  • This is redirecting a specific subdomain to the main domain, for a specific file.

    Which version of isapi_rewrite are you using, v2 or v3?

    This assumes v3:

    RewriteCond %{HTTP:Host} ^subdomain\.(.*)$
    RewriteRule ^(/foo/foofile3\.txt)$ http://%1$1 [NC,R=301]
    

    The RewriteCond tests for a host beginning with subdomain (and a dot), and captures the remaining (example.com) for convenience in the rewrite rule. You could also put the specific example.com in there, but this keeps it more generic.

    The RewriteRule then looks for a specific /foo/foofile3.txt, and captures it (again for convenience of not repeating it in the rule). The result has %1 for the capture up in the condition (example.com) and $1 for the capture in the rule (/foo/foofile3.txt)

    The NC is not case-sensitive, the R=301 is a permanent redirect.

    With v3, querystring parameters are automatically handled, so this keeps any that are after the file.

    Other possibilities that make it a little more complex are any subdomain at all, or filename patterns instead of the one specific file.

    I was having trouble with the browser caching my attempts while getting the rule right. So even though I changed the rule, the browser had cached my previous redirect. My quick fix was to count up the filename for new urls: foofile2, foofile3 (I guess I got it on the 3rd try...). I could have cleared the browser cache each time too; this was quicker.