0

I want to shut down a website and redirect all pages to the homepage of another, already existing, website.

This is my code in the .htaccess of the website to be shut down:

RedirectMatch 301 ^/ https://www.new-domain.com/

On the target page I want to set a query for visitors of any page of the decommissioned website, which will output an informational text exclusively for those visitors.

Here is the php code I use for this:

if (false !== stripos($_SERVER['HTTP_REFERER'], "old-domain.com")):
    echo 'Text, exclusively for visitors coming via a URL of the old site';
else:
    // echo '';
endif;

Unfortunately, the query only works as long as I haven't set the redirect in the decommissioned site's .htaccess. It seems that the script does not recognize where the visitors are actually coming from.

How can this be solved?

anyway
  • 27
  • 7
  • So what does `($_SERVER['HTTP_REFERER']` contain in that case? – arkascha Dec 17 '20 at 07:17
  • And why not a simple `Redirect permanent / https://www.new-domain.com/` ? – arkascha Dec 17 '20 at 07:22
  • Is the old domain still going to survive? I am asking this because your .htaccess is surviving in old domain. I would suggest this echo script in old domain and redirect via javascript to new domain, say after 5-10 seconds – nice_dev Dec 17 '20 at 07:25
  • @arkascha Thanks for replying! $_SERVER['HTTP_REFERER'] contains NULL. With a change to "Redirect permanent" this does not work either. – anyway Dec 17 '20 at 08:02
  • 1
    @nice_dev Thanks for replying! Yes, the old domain should survive permanently. Your suggestion sounds interesting, but I would try to solve it with PHP first. If that doesn't work, I'll be very happy to come back to your approach. – anyway Dec 17 '20 at 08:06
  • [You shouldn't rely on `HTTP_REFERER` being there](https://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty) - antivirus, firewalls, proxies can all remove it. You could instead redirect with a query string, like `?redirected=y`, and test for that on your target site. – Don't Panic Dec 17 '20 at 09:03
  • Most likely your old domain performs _two_ redirections. That would explain why you get no referrer. So you will need to start debugging, what actually happens if you request a URL pointing to the old domain in those cases. Easiest starting point is the network tab in the development console in your browser when using a new anonymous browser window: what redirections do you see? – arkascha Dec 17 '20 at 09:44
  • @Don'tPanic This is a very good idea and works perfectly. This even has the advantage that the element disappears when the page is switched. Thanks a lot for your input! – anyway Dec 17 '20 at 10:04

1 Answers1

0

I have now implemented it like this (with best thanks to @Don'tPanic)

.htaccess:

RewriteRule ^(.*)$ https://www.new-domain.com/?redirect=myID [R=301,L]

PHP:

if (htmlspecialchars($_GET["redirect"]) == 'myID'):
    echo 'Text, exclusively for visitors coming via any URL of the old site';
else:
    // echo '';
endif;
anyway
  • 27
  • 7