skip to Main Content

I currently have .htaccess file that lists dozens of domains to enable CORS to from my server.

I shortened my example below , but all the domain names are similar and the only part of the domain name that is changed is the 1 or 2 digit number after the www.

<IfModule mod_headers.c>
    SetEnvIf Origin "http(s)?://(www48.example.com||www47.example.com)$" AccessControlAllowOrigin=$0
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
        Header merge Vary Origin
</IfModule>

so in this example i have CORS enabled for

https://www48.example.com
https://www47.example.com

I was wanting a simpler way to enable a list of of 99 domains with similar names. So all domain names are identical aside of digits 1 to 99 after the "www" , how can I achieve this without listing all 99 domain names individually?

https://www1.example.com
https://www2.example.com
....
https://www10.example.com
....
https://www40.example.com
....
https://www70.example.com
....
https://www99.example.com

2

Answers


  1. Chosen as BEST ANSWER

    This seems to work , anyone review and see if ok

    <IfModule mod_headers.c>
        SetEnvIf Origin "http(s)?://(www[1-9][0-9]?.example.com)$" AccessControlAllowOrigin=$0
            Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
            Header merge Vary Origin
    </IfModule>
    

  2. You can achieve this by using a regular expression in the SetEnvIf Origin line. Your .htaccess file would look like this:

    <IfModule mod_headers.c>
        SetEnvIf Origin "http(s)?://(www[1-9][0-9]?.example.com)$" AccessControlAllowOrigin=$0
        Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
        Header merge Vary Origin
    </IfModule>
    

    In the regular expression www[1-9][0-9]?.example.com, the [1-9][0-9]? part matches one or two digits from 1 to 9. Here is how it works:

    1. The [1-9] part matches any single digit from 1 to 9.
    2. The [0-9]? part matches any single digit from 0 to 9 or no digit at all. The ? makes this part optional.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search