0

Using .htaccess, I am catching unfound pages and sending them to 404.php My site used to be ASP.net and is now php. Most of the links are corrected in .htaccess for .aspx pages to .php, but I have some coming in which are case insensitive. For example MyPAGE.aspx need to be MyPage.php.

My 404 page is showing, and in it I can adjust the uri as appropriate and display the link. But it will not redirect by itself.

My code in the 404 file is as such.

<?php
     $uri = $_SERVER["REQUEST_URI"];

     echo "<p>" . $uri . "</p>";

     $pos = stripos($uri, "mypage.php");
     if ($pos)
        {
            $uri = str_ireplace ("mypage.php", "MyPage.php", $uri);
            header('Location: ' . $uri);
            echo sprintf("<p>Please try : <a href='%s'>%s</a></p>",$uri,$uri);

        }

    ?>

Instead of redirecting, it is showing the link - which works.

StripyTiger
  • 877
  • 1
  • 14
  • 31
  • FYI: familiar with [mod_speling](https://httpd.apache.org/docs/2.4/mod/mod_speling.html)? – ficuscr Mar 08 '18 at 17:38
  • btw, that echo will never happen. You can't echo and use a header together. – Funk Forty Niner Mar 08 '18 at 17:39
  • 1
    You cannot make header() after you had some output (echo). This doesn't work. You probably have error_reporting to off, so you don't see the error. – jonas3344 Mar 08 '18 at 17:39
  • This also seems similar - https://stackoverflow.com/questions/6142244/php-header-location-404-php-true-404-does-not-work – adprocas Mar 08 '18 at 17:42

2 Answers2

0

Redirecting using header request (a 302) doesn't work after already sending a 404, from what I understand.

Instead, do an meta refresh.

<meta http-equiv="refresh" content="0;url=<?php echo $uri; ?>" />

Yours could be:

<?php
     $uri = $_SERVER["REQUEST_URI"];

     echo "<p>" . $uri . "</p>";

     $pos = stripos($uri, "mypage.php");
     if ($pos)
        {
            $uri = str_ireplace ("mypage.php", "MyPage.php", $uri);

            echo sprintf("<meta http-equiv=\"refresh\" content=\"0;url=%s\">",$uri);
            echo sprintf("<p>Please try : <a href='%s'>%s</a></p>",$uri,$uri);

        }
?>
adprocas
  • 1,863
  • 1
  • 14
  • 31
0

Remove all output before header and exit after header

<?php
 $uri = $_SERVER["REQUEST_URI"];
 $pos = stripos($uri, "mypage.php");
 if ($pos)
    {
        $uri = str_ireplace ("mypage.php", "MyPage.php", $uri);
        header('Location: ' . $uri);
        exit;
    }
  ?>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109