Detecting headers in redirects

Hi,

I have a site setup as so:

www.domain.com is my main site, not on netlify
netlify.domain.com is my site on netlify

Any requests to www.domain.com/directory are proxied to netlify.domain.com/directory using a cloudflare worker (netlify.domain.com is NOT proxied by cloudflare at the moment)

This is working as it should.

However, I’d like to block direct access to netlify.domain.com - or at least redirect any requests netlify.domain.com/* to www.domain.com

Is there a way to do this using the _redirects or netlify.toml file?

My fallback option is to run a second worker on cloudflare that can detect if my proxy is doing the request (by sending and detecting an X-proxy=www.domain.com type header in the two worker scripts respectively)

Cheers,

Guy

2 Likes

Hello, @guybowden, and welcome to our Netlify community site. :slight_smile:

In _redirects format, this can be done using the following:

https://example.netlify.com/* https://example.com/:splat 301!

This forces a 301 redirect for any attempt to load the site via example.netlify.com . To get this to work, one must replace the target domain and *.netlify.com subdomain with the appropriate values for your site.

​Please let us know if there are other questions or if this doesn’t work as promised.

Thanks for that - however I’m not sure it would address the issue where I do want example.netlify.com to work as normal (i.e. no redirects) if the request is coming via my reverse proxy. But not for a direct browser request.

@guybowden, I missed that you wanted to allow it via proxy to Netlify.

First, I must mention that we do not support being proxied to. That being said, people do use proxies in front of their Netlify sites but we do not provide technical support for these configurations.

You can use Netlify as a proxy to another service and we provide support for that. Again, though, we do not support the reverse configuration.

Regarding being able to conditionally redirect based on the presence (or absence) of an HTTP request header, we have an open feature request for this. I’ve cross linked the feature request to this community topic for tracking and we’ll reply here, if/when the issue is resolved.

Other people finding this post, please :heart: the original post above if you want to see this feature request as well.

Thanks for the update. I will look at doing it a different way for now.

1 Like