Set a request condition to redirect URL

Comments

1 comment

  • Eden

    Hi Rachel,

    Can you let me know why it doesn't work? How exactly are you setting things? I would recommend creating VCL snippets in order to handle redirects. 

    Here is a developer doc that covers doing redirects with tables: 

    https://developer.fastly.com/solutions/tutorials/redirects/

    However, you can create even simpler redirects using VCL snippets:


    #vcl_recv
    if (req.url ~"^/abr_live"){
       error 601 "custom error message";
    }

    #vcl_error
    if (obj.status == 601) {
       set obj.status = 301;
       set obj.response = "Moved Permanently";
       set obj.http.Location = "https://" <new_hostname_here> req.url;
      return (deliver);
    }

    In the above code, we set a condition to look for that url and then error with a custom error code. That sends the request to vcl_error where we set a condition to look for the custom error code and do the redirect at that point, making sure to also set the proper status code and response message. 

    The redirect happens with the "set obj.http.location" variable. You'll want to make sure to modify that to use the protocol you'd prefer (https:// or http://), set the new domain name or hostname, and either keep the current req.url or change it if needed. An example of what that might look like is this:

    set obj.http.Location = "https://" "subdomain.example.com" "/" ;
    #or (the + character is optional)
    set obj.http.Location = "https://" + "subdomain.example.com" + req.url ;

    Please let us know if that helps!

    0
    Comment actions Permalink

Please sign in to leave a comment.