Redirect www. to non www. not working

Hi,

I have quite an advanced HAP v1.5 config going but can’t seem to get to strip away www. from all http requests. I have multiple sub domains all under a wildcard cert and I can not have any www. The structure is as follows:
abc.example.com
def.example.com
ghi.example.com
… etc

This does go on to the ssl part afterwards. This is what I have to strip away any www. first:

http-request add-header X-Host-Redirect yes if { hdr_beg(host) -i www. }
acl host_redirect hdr_cnt(X-Host-Redirect) eq 1
reqirep ^Host:\ www.(.*)$ Host:\ \1 if host_redirect
redirect code 301 scheme http if host_redirect

Then it goes on to the ssl as follows:

frontend 192-168-0-18-80 192.168.0.18:80
redirect scheme https code 301 if { hdr(Host) -i abc.example.com } !{ ssl_fc }
redirect scheme https code 301 if { hdr(Host) -i def.example.com } !{ ssl_fc }
redirect scheme https code 301 if { hdr(Host) -i ghi.example.com } !{ ssl_fc }
…etc

then the acl’s and backends.

Could someone please help me understand why I just get a 503 error if I use www.abc.example.com
Thanx in advance.

Ok so I found the solution. The way I did it was by putting in the www. redirect within the frontend. It now works as follows:

frontend 192-168-0-18-80 192.168.0.18:80
acl host_www hdr_beg(host) -i www.
reqirep ^Host:\ www.(.*)$ Host:\ \1 if host_www
redirect code 301 prefix / if host_www
redirect scheme https code 301 if { hdr(Host) -i abc.example.com } !{ ssl_fc }
redirect scheme https code 301 if { hdr(Host) -i def.example.com } !{ ssl_fc }
redirect scheme https code 301 if { hdr(Host) -i ghi.example.com } !{ ssl_fc }
…etc