简体   繁体   中英

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM