IIS 6 Isapi filter - forward or redirect requets to other domain

641 Views Asked by At

A code bellow is main part of my Isapi filter for IIS 6. I need to redirect all request which contains "/some_string" to other url, which lies on other domain and other server. How to do it?

DWORD CAmgnIsapiFilter::OnPreprocHeaders(CHttpFilterContext* pCtxt,
    PHTTP_FILTER_PREPROC_HEADERS pHeaderInfo)
{
    char buffer[256];
    DWORD buffSize = sizeof(buffer);
    BOOL bHeader = pHeaderInfo->GetHeader(pCtxt->m_pFC, "url", buffer, &buffSize); 
    CString urlString(buffer);
    urlString.MakeLower();

    if (urlString.Find("/some_string") != -1) //we want to redirect this file
    {
        urlString.Replace("/some_string","");
        urlString = "http://new_domain.cz/something" + urlString;

        char * newUrlString= urlString.GetBuffer(urlString.GetLength());
        pHeaderInfo->SetHeader(pCtxt->m_pFC, "url", newUrlString);
        return SF_STATUS_REQ_HANDLED_NOTIFICATION;
    }
    //we want to leave this alone and let IIS handle it
    return SF_STATUS_REQ_NEXT_NOTIFICATION;
}

Thanks!

1

There are 1 best solutions below

0
On

You need to respond with a 302 status code, telling the browser which is the new URL you want to redirect to.

Just a small, not production-grade template to achieve this:

//... char rsp[50]; wsprintf(rsp, "Location: %s\r\n\r\n", newUrlString); pfc->ServerSupportFunction (pfc, SF_REQ_SEND_RESPONSE_HEADER, (PVOID) "302 Redirect", (DWORD) rsp, 0); //...

instead of

pHeaderInfo->SetHeader(pCtxt->m_pFC, "url", newUrlString);

I think this is the only thing you have to do.