Redirect sub domain

Hello! :wave:

How do I redirect from mysubdomain.mydomain.com to myotherdomain.com?

I already have a _redirects file redirecting path slugs but I want to redirect this sub domain to a new domain now.

Hi, @spences10, and welcome to our community site.

Two rules are recommended, one each for HTTP and HTTPS urls respectively. Using the examples you provided, the rules would look like this:

http://mysubdomain.mydomain.com/* https://myotherdomain.com/:splat 301!
https://mysubdomain.mydomain.com/* https://myotherdomain.com/:splat 301!

Both rules direct to HTTPS because I’m assuming both domains are at Netlify. We don’t support non-SSL HTTP connections - only HTTPS.

If the target site isn’t at Netlify, though, feel free to redirect HTTP to HTTP if you prefer. At Netlify though, the target URL should always be HTTPS.

The 301! in the rule tell Netlify’s CDN to issue an HTTP redirect (a 301 status) which will change the URL in the address bar.

The ! (in 301!) means always redirect, even if the asset exists at the original URL (which is the most commonly used configuration). If the ! is removed, the redirect won’t trigger if there if the URL is valid at the starting domain (which is probably not what you want).

Note, this will only work if starting domain (mysubdomain.mydomain.com) has a DNS configuration which directs this traffic to Netlify. Directing the domain to Netlify can be done with either Netlify DNS or external DNS configuration.

​Please let us know if there are other questions about this or the redirect above doesn’t work as expected.

1 Like

Thanks @luke

I’ve added that to the project _redirects it’s not immediately redirecting so I’m presuming DNS magic needs to happen first.

If dns isn’t pointed to us then indeed that pattern won’t work. If it does get pointed to us and still doesn’t work, please do let us know your URL so we can take a closer look at your configuration.

Hey @fool, so the domain is ss10.dev and the DNS is configured from Namecheap to Netlify.

I want to point the sub domain of cheat-sheets.ss10.dev to cheatsheets.xyz which is not currently on Netlify.

At the moment this is what my _redirects file looks like:

http://cheat-sheets.ss10.dev/*  https://cheatsheets.xyz/:splat 301!
https://cheat-sheets.ss10.dev/* https://cheatsheets.xyz/:splat 301!

This doesn’t seem to do the thing right now though.

Thanks

@spences10, I think we are closer to a solution! The DNS records to point to Netlify so that is not the root cause.

I found part of the issue here:

https://app.netlify.com/sites/jovial-brown-7e7758/deploys/5db97e0c703af6da8c9c6a0c

Quoting that page:

No redirect rules processed
This deploy did not include any redirect rules. Learn more about redirects.

I then used the download deploy button on that page to examine the deployed files. Please see the screenshot below for an example of where to find the button.

When I extract the zip file, I find no file named _redirects in it. Are you certain this file is being uploaded to Netlify? If not, would you please test to deploying again - making sure to include this file in the base directory of the uploaded files?

If this doesn’t resolve it or there are any questions, we’re happy to follow-up again so please let us know if this is the case.

1 Like

Ok, maybe I should have paid attention to that before posting! :upside_down_face:

So, I built my site again, locally, and tried again and the _redirects doesn’t go into the build when building locally for some reason??

What I’m going to to is revert to the GitHub integration, push the file to GitHub and see if that does the trick.

So, although, for some reason even when connecting a GitHub repo the redirects file still wasn’t being shown in the deploy summary.

What I did was after building the site locally, chucked the _redirects file into the public folder then manually deployed that.

Added the sub domain to that and tried again, this time it appears to redirect but the URL doesn’t change is that the 301 splat thing?

I get the 301 redirect when I test now:

$ curl -sv https://cheat-sheets.ss10.dev/  2>&1 | egrep "^<.*$"
< HTTP/2 301
< cache-control: public, max-age=0, must-revalidate
< content-length: 40
< content-type: text/plain; charset=utf-8
< date: Fri, 01 Nov 2019 07:02:35 GMT
< location: https://cheatsheets.xyz/
< age: 7
< server: Netlify
< x-nf-request-id: 73784824-9b29-43db-9c85-6f67788617bf-8775509
<

This does change the domain/URL in the address bar when tested with a web browser also.

Did you resolve this issue? If it still isn’t working when you test, would you please send the x-nf-request-id header for a page load which doesn’t work?