-1

We have moved our website to a new domain and our SEO department want to have some redirect from the old urls to the new urls with 404 status code. I dont know if it is possible and want to collect some opinions from the community

My question: i want to try to configure a redirect from a internal url to a external URL with 404 as status code in Nginx. I have tried with rewrite and return. But it looks like that it's imposible.

  • rewrite can only return 301 or 302 as status code.
  • return can't do any redirect with 404.
  • I have tried with rewrite last and return 404. But it didn't work, because i want to redirect to external URL.

Anyone has some idea?

qvpham
  • 109
  • 1
  • 7
  • You can’t redirect with 404. You could use JavaScript (or meta tag) to make browser to navigate to another page after receiving 404 page. – Alexey Ten Feb 18 '20 at 05:17
  • Thank you, i need it for a SEO requirement. Because of that is a client redirect with JS not useful for me. – qvpham Feb 18 '20 at 09:48
  • 2
    SEO requirements can’t change how HTTP protocol works – Alexey Ten Feb 18 '20 at 13:46
  • 1
    What is your SEO requirement? There may be an actually possible way to achieve it. – Mike Scott Feb 18 '20 at 17:52
  • @MikeScott: We have moved our website to a new domain and our SEO department want to have some redirect from the old urls to the new urls with 404 status code. I dont know if it is possible and want to collect some ideas from the community – qvpham Feb 19 '20 at 09:41
  • @AlexeyTen: please see my comment above... – qvpham Feb 19 '20 at 09:41
  • So what's wrong with JS/Metatag solution? They'll have 404 and eventually user will be redirected to any page you want. – Alexey Ten Feb 19 '20 at 09:45
  • 1
    Still, I can't understand why would anyone want 404 *and* redirect. If you know new location of an old url, just redirect user there. No need for 404. – Alexey Ten Feb 19 '20 at 09:47
  • @AlexeyTen Do you mind redirect with JS? You know, SEO is for search engine and some SE can't work with JS correctly. Other way: we want, that it works from the server side. – qvpham Feb 19 '20 at 09:47
  • @AlexeyTen: i said exactly the same to our SEO department but still want to collect more opinions about this topic. – qvpham Feb 19 '20 at 09:49
  • 1
    I'm voting to close this question as off-topic because ye canna change the laws of physics. – womble Feb 19 '20 at 09:58

3 Answers3

2

You can’t. 404 isn’t a redirect status code, it’s an error status code, and so web browsers that receive it will treat it as an error rather than a redirect. If you want to say that the page isn’t there any more and send the browser to find it somewhere else, that’s what the 301 code is for.

Mike Scott
  • 7,903
  • 29
  • 26
1

A 404 can be done only internally it can't be done for an external URL. You can only direct it but not with a 404. A 404 means that it's not there or dosen't exists.

If both servers or urls belongs to you, you can redirect to a php page with a php header of 404 and a refresh html meta that would redirect to the new URL. But this is not the right way to redirect

Talal Al-Khalifa
  • 648
  • 5
  • 12
1

You cannot control the HTTP status code of an external page.

However, if what you want to achieve is displaying external page's content with 404 status on your site, this is totally possible. You can use proxy_pass.

But it gets more tricky if the remote location emits 200. Two locations would be needed:

location = /foo {
    error_page 404 = @ext_404;
    return 404;
}

location @ext_404 {
    proxy_pass https://bar.example.com;
    rewrite .* /lorem-ipsum break;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host bar.example.com;
    proxy_set_header X-NginX-Proxy true;
    proxy_redirect off;
    proxy_connect_timeout 1;
    proxy_intercept_errors off;
    expires 30;
}

As a result, when someone visits https://your.example.com/foo, they will actually see the contents of https://bar.example.com/lorem-ipsum with 404 status code.

Danila Vershinin
  • 4,738
  • 3
  • 16
  • 21
  • Thank you for your reply. But it was a requirement, that it redirects to a new URL. I don't want to control the status code of the new external URL. For example: If some one visits `https://your.example.com/foo`, it returns 404 for `https://your.example.com/foo` and do a redirect to `https://new.example2.com/foo`. So the new URL `https://new.example2.com/foo` can have a new status code, musst not be 404. – qvpham Feb 17 '20 at 17:12
  • Your requirement would be more clear if you can answer: what is the final URL to be shown in the browser address bar? your site's URL displaying external contents, or an external URL with external contents? – Danila Vershinin Feb 17 '20 at 17:14
  • It should be **an external URL with external contents** – qvpham Feb 17 '20 at 17:16
  • Well, then you can't do anything about it as you are not in control of the external server. – Danila Vershinin Feb 17 '20 at 17:17
  • May be u understood it false. I don't want to do anything with the external server. I want to change set a status code 404 for the url of my server and return a redirect to the new server. Something like that `rewrite mysite.com newsite.com [404]` – qvpham Feb 17 '20 at 17:26
  • It is simply impossible by HTTP specification. There is no 404 + redirect URL. No browser supports this scenario. You can only supply redirect URL with 301 and 302. – Danila Vershinin Feb 17 '20 at 17:28
  • Ok. I hear about this. But want to try to find a way to do it. Thank you very much! – qvpham Feb 17 '20 at 17:32