How Does vcl_error Work?

Comments

3 comments

  • Udaya Kumar

    Hi Squee,

    We have a requirement  to redirect any requests only from https://www.pn.com/ or https://www.pn.com/default.aspx to https://www.dn.com/pn/

    I have tried various methods (vcl & error) in the fastly fiddle and did not work as per the requirement.

    Here dn.com is the magento2 website where we use Fastly CDN.

    -U

    0
    Comment actions Permalink
  • Squee

    Hi Udaya,

    Thanks for reaching out. 

    We're happy to look at this with you, but I suspect we'll need more details to provide you with a solution. 

    As for the fiddle, the host on the fiddle is some variation on `exec<number>.fiddle.fastlydemo.net`. If you want to write VCL to be able to test redirects coming in from another host, try setting that host at the top of `vcl_recv`. That would look something like:

    set req.http.host = "www.pn.com";

    You would only want to do this in the fiddle to create the testing environment you need. Please don't put this line in your code unless you intend to rewrite the host for your origin to find.

    If you have further questions, please don't hesitate to open a ticket with support by emailing support@fastly.com.

    0
    Comment actions Permalink
  • Anish Asokan

    Hi Squee

     

    Thank you so much Squee, This was a real life-saver for me.

    And this worked perfectly fine on my magento-cloud where we have fastly.

     

    We recently migrated a magento1 website from their old server to magento2 in magento-cloud.

     

    So, I was in a situation where I needed a custom URL redirect for:

     

    `mcstaging.magento-cloud.com/us/<anything>` should be redirected to `mcstaging-us.magento-cloud.com/us/<anything>`

     

    I basically made use of your rule:

     

    in `vcl_recv`

    # 301 Redirects
    if (req.http.host == "mcstaging.magento-cloud.com" && req.url ~ "^/us/(.*)") {
    error 607 "Custom RegEx 301 Redirect";
    }

     

    in `vcl_error`

     

    # 301 Redirects
    if (obj.status == 607) {
    set obj.status = 301;
    set obj.response = "Moved Permanently";
    set obj.http.Location = "https://mcstaging-us.magento-cloud.com/" req.url;
    synthetic {""};
    return (deliver);
    }

     

    This helped me with redirecting all old `/us/` store URL's to be redirected to new US store domain `mcstaging-us.magento-cloud.com`

     

    This documentation was really helpful.

    Thanks,
    Anish

    0
    Comment actions Permalink

Please sign in to leave a comment.