htaccess add response header based on request header

Softlayer will cache and pass on any http response headers we send when they request a file.

this is what normal request header variables look like
Connection || keep-alive
Content-Length || 0
Accept || text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding || gzip, deflate, sdch
Accept-Language || en-US,en;q=0.8
Cookie || LandingPage=/custompage.aspx; ASP.NET_SessionId=m3gioye5ckds1baqfhgpaea0
Host ||
www.trilogyplastics.com
Referer || http://www.trilogyplastics.com/
User-Agent || Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Upgrade-Insecure-Requests || 1
X-REQUEST-URI || /rotomold-capabilities.aspx
X-Rewrite-Url || /rotomold-capabilities.aspx
Original-Url || /rotomold-capabilities.aspx
X-SUBREQEST-LEVEL || 1

 

this is what SoftLayer CDN request header variables look like

Via || HTTP/1.1 ECAcc (dca/53EA)
Content-Length || 0
Accept || text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding || gzip, deflate
Accept-Language || en-US,en;q=0.8
Host ||
www.trilogyplastics.com
User-Agent || Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Upgrade-Insecure-Requests || 1
X-Forwarded-For || 208.40.106.42
X-Host || cdn.trilogyplastics.com
X-Forwarded-Proto || http
X-REQUEST-URI || /rotomold-capabilities.aspx
X-Rewrite-Url || /rotomold-capabilities.aspx
Original-Url || /rotomold-capabilities.aspx
X-SUBREQEST-LEVEL || 1

 

I'm using the X-Host to see if it starts with "CDN." or has ".CDN." in it, if so then add the canonical link to the response header

 

# ###################################################################################
# ####### CDN = Set Canonical Link in HTTP Response Header #######
# ###################################################################################
#set environmental variable for the filename
RewriteCond %{HTTPS} (On)?
RewriteRule .* - [E=FILENAME:http(?%1s)://%{HTTP_HOST}%{REQUEST_URI}]
#check if the request header variable x-host exists, and if it does then starts with cdn.
RewriteCond %{HTTP:X-Host} ^.*\.?cdn\..*$
RewriteRule .* - [E=AddCanonicalForCDN:true]
#Add Canonical Header if environmental variable AddCanonicalForCDN exists
Header add Link '%{FILENAME}e; rel="canonical"' env=AddCanonicalForCDN


 

Add Feedback