Setting origin or referer value which from request

2019-09-18 00:21发布

问题:

How to set origin value or referer value in a response-header using tuckey url rewrite filter xml file.

Accessing %{origin} directly in the set tag is not working but not working in condition tag.

Below is the example I tried.

    <rule enabled="true" last="false" match-type="regex">
    <name>Enabling CORS Headers</name>
    <from>^/path/someMorePath.*$</from>
    <condition name="origin" operator="equal">http://www.testsite.com</condition> <!-- would like to write some regex to match a set of sites and if matched, then set the same origin as response -->
    <set type="response-header" name="Access-Control-Allow-Origin">%{origin}</set>
    <set type="response-header" name="Access-Control-Allow-Credentials">true</set>
</rule>

回答1:

After having an complete understanding on the urlrewritefilter of tuckey api, the solution is to have a regular expression.

<rule enabled="true" match-type="regex">
    <name>Enabling CORS Headers</name>
    <from>^/path/someMorePath.*$</from>
    <condition name="origin" operator="equal">([a-z.\/\/0-9]+)</condition>
    <set type="response-header" name="Access-Control-Allow-Origin">%1</set>
    <set type="response-header" name="Access-Control-Allow-Credentials">true</set>
</rule>

So, here using the regular expression in the condition tag and then using the back references in the regular expression as the variables in the response header