0

I am using .htaccess to get the Referrer URL. For example, if someone clicks the link example.com/dogfood-post-1 and the post is not found, how can I get that URL example.com/dogfood-post-1 in my 404.php page?

ErrorDocument 404 /404.php
TRiG
  • 10,148
  • 7
  • 57
  • 107
hd flix
  • 17
  • 3
  • Does this answer your question? [PHP: How to get referrer URL?](https://stackoverflow.com/questions/16374704/php-how-to-get-referrer-url) – maio290 Apr 16 '20 at 12:55

2 Answers2

0

Not really a full answer as it depends on specific configuration, but this should help you.

Add var_export($_SERVER) to your 404.php and see which key contains the value you need.

astax
  • 1,769
  • 1
  • 14
  • 23
  • If a person click on a post and that post is not found. Example domain.com/post-about-cat and that post is not found, how can i get the domain.com/post-about-cat inside the 404.php page? – hd flix Apr 16 '20 at 13:06
  • @hdflix can you put your custom code to that 404.php? If yes, add `var_export($_SERVER)` as I suggested, open a non-existing post (so you get the 404 page) and look at the output. Look for the original URL you requested. Then you'll be able to use is as $_SERVER[...some key...]. Very likely this will be $_SERVER['REQUEST_URI'] or $_SERVER['REDIRECT_URL'], but this really depends on how exactly you've configured redirect to 404.php (hint - there is way more than one method). – astax Apr 16 '20 at 16:36
0

If you just want the url on the domain.com/dogfood-post-1 you could use:

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

source: get full url stackoverflow

if you send them to another page you could set a session and put the url in the session and then display it on the 404 page.

Instead of redirecting users to a 404 page you should show them the 404 error page. In your .htaccess you can add:

ErrorDocument 404 /appName/404.php

Put your absolute path before the 404.php in order to show it properly.

TimeParadox
  • 197
  • 1
  • 2
  • 16
  • using this I am Getting the domain.com/404.php, If i have a post, domain.com/blog/pet-food You click on it but the post is not found i have a htaccess file inside the blog folder ErrorDocument 404 /pages/404.php, Do i have to do something inside the htaccess ? maybe ErrorDocument 404 /pages/404.php?id=%{HTTP_REFERER} – hd flix Apr 16 '20 at 13:14
  • If you are getting `/404.php` as the requested URI, then your server must have actually _redirected_ to the ErrorDocument - which it should not do in the first place. (Because then search engine bots & co. will not recognize this as a proper 404 any more, because they see the 200 OK status code from the “new” URL. And even if that script responded with a 404 status code itself, it would still be for the wrong URL now.) Guess you probably have more stuff in your .htaccess than just this? If so, please edit your question, and show the full thing. – CBroe Apr 17 '20 at 07:29
  • @hdflix I have updated my awnser so you can show the 404 page instead of redirecting – TimeParadox Apr 17 '20 at 09:42