Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Redirect on 404 Error


drakonan

Recommended Posts

Hello, I need to compare the visitor's typed URL with a list of redirect URLs, if its found redirect user to "moved" URL. If it doesn't exist, "Page cannot be found" and a link to my home page would be great.

 

This guy from the net is giving out the way he does it in PHP, but as I don't understandPHP I don't understand what I need to do to get up which URLs are redirect URLs...

 

<html>

<head>

<title>File Not Found</title>

</head>

<body>

<h2>File Not Found</h2>

<p>You have tried to access a page that does not exist on the <a href="http://norfolkgraphics.com">Norfolk Graphics</a> website.</p>

<?



 $oldURL = $GLOBALS["REDIRECT_REDIRECT_ERROR_NOTES"];

 $lastSlashLoc = strrpos($oldURL, "/");

 $firstSlashLoc = strpos($oldURL, "/");

 $oldFileName = substr($oldURL, $lastSlashLoc+1, strlen($oldURL)-$lastSlashLoc-1);

 $basePath = substr($oldURL, $firstSlashLoc, $lastSlashLoc-$firstSlashLoc+1);



 $dotloc = strpos($oldFileName, ".");

 $newFileBase = substr($oldFileName, 0, $dotloc);



 $fullNewFileName = $basePath;

 $fullNewFileName .= $newFileBase;

 $fullNewFileName .= ".php3";

  

 if (file_exists($fullNewFileName))

   print("The file you were looking for has moved <a href="$newFileBase.php3">here</a>n");



?>

</body>

</html>

- - - -

Sometimes, ignorance is bliss.

Link to comment
Share on other sites

That's it. I'm going to learn this PHP stuff before I die.

 

Why can't I simply echo print REDIRECT_URL?

 

<HTML>

<BODY>

 

<?php

print('The page ' . $REDIRECT_URL . ' cannot be found.');

?>

 

</BODY>

</HTML>

 

ALL of the redirect variables are blank. redirect_status etc...

 

Is there some setting I have to turn on?

- - - -

Sometimes, ignorance is bliss.

Link to comment
Share on other sites

Brandon, Here's a simple example to redirect .html files to .php with the same name and then default to the home page if the .php doesn't exist either.

<?php

$url = $HTTP_GET_VARS['url'];

$extension = strstr( $url, '.html' );

if ( $extension )

{

  $url = str_replace( '.html', '.php', $url );

  header( 'Location: http://www.arbucklechurch.org' . $url );

  exit;

}

header( 'Location: http://www.arbucklechurch.org' );

?>

Here's a example of a 404.php to handle a small number of redirects. I think it's easier to understand than the other one.

<?php

$url = $HTTP_GET_VARS['url'];

if ( $url == '/PaulDodd.html' )

{

  header( 'Location: http://www.oakvalley.com/PaulDodd.php' );

  exit;

}

if ( $url == '/Link.html' )

{

  header( 'Location: http://www.oakvalley.com/Clients.php' );

  exit;

}

if ( $url == '/oakvalley.html' )

{

  header( 'Location: http://www.oakvalley.com/Status.php' );

  exit;

}

header( 'Location: http://www.oakvalley.com' );

?>

In olden times the men were made of iron and the ships were made of wood; now it's the other way around. :wink:

Link to comment
Share on other sites

Thanks for the code but I still have the same problem... for some reason the variables don't have anything in them. The page just redirects always since the variable url is null...

 

Would you (or anyone else) know why this is?

- - - -

Sometimes, ignorance is bliss.

Link to comment
Share on other sites

How are you making the connection to 404.php? On my setup I had to make a 404.shtml containing this

<script language="javascript">

<!--

window.location="/404.php?url=<!--#echo var="REQUEST_URI" -->";

-->

</script>



<!-- a bunch of junk to make the file 10KB -->

and then setup 404.shtml as my 404 error handler. One of my hosting companies had a note that if your 404 handler wasn't 10KB it wouldn't work with IE. It seemed to be true at least at that host. I don't know if this is helpful, but it works for me at notAllowedToSay.com and at alsoNotAllowedToSay.com. :wink:

In olden times the men were made of iron and the ships were made of wood; now it's the other way around. :wink:

Link to comment
Share on other sites

Hey Orchard...

 

Even if I just type

 

<script language="javascript">

<!--#echo var="REQUEST_URI" -->";

</script>

<!-- 10kb of junk -->

 

as my 404 page, the variable is null, so the page is blank!

 

It makes no sense... I've tried this both locally and online...

- - - -

Sometimes, ignorance is bliss.

Link to comment
Share on other sites

How do you tell the server to use your 404 page? One of my hosts uses MonsterControls and the other uses a Control Panel. In both cases I set up the 404 page through their control pages and not by editing .htaccess or something directly. I wonder if there is something you have to do to ask the server to pass the variable to you. I had my 404 page set as 404.shtml and had to change it to /404.shtml or else it did an infinitely loop for 404's that weren't in the root directory.

In olden times the men were made of iron and the ships were made of wood; now it's the other way around. :wink:

Link to comment
Share on other sites

My hosting company uses a front for .htaccess ... Its basically similar to how the new osCommerce lets you edit files without having to directly...

 

I simply place the ErrorDocument 404.... You've got me thinking about this... How do you have it forward? I simply pointed it direct to 404.shtml

and from there had it attempting to place the requested url in a variable for 404.php...

- - - -

Sometimes, ignorance is bliss.

Link to comment
Share on other sites

I used my hosts web interface to choose /404.shtml as the 404 error page. I have 404.shtml as shown about 5 posts back only where it says "a bunch of junk to make the file 10KB" I have 5 lines that are 2000 characters long containing "0123456789" 200 times in a row. The java script in that file redirects to /404.php which is the first of the two code sections about 10 posts back. It tries to replace .html on the end of the file with .php and if that doesn't exist either it goes to the main page of the web site.

In olden times the men were made of iron and the ships were made of wood; now it's the other way around. :wink:

Link to comment
Share on other sites

Ok, and your site does just that? You have tested it?

 

I even emailed my webhost tech support and after looking at everything he thinks it can't be done. Can you give me a site that I can show to him that it does work (With a "bad" url that gets directed)

- - - -

Sometimes, ignorance is bliss.

Link to comment
Share on other sites

Certainly,

www.oakvalley.com/PaulDodd.html will 404 to www.oakvalley.com/PaulDodd.php

www.oakvalley.com/junk.html will 404 to www.oakvalley.com

www.arbucklechurch.org/Directions.html will 404 to www.arbucklechurch.org/Directions.php

www.arbucklechurch.org/junk.html will 404 to www.arbucklechurch.org

 

I think you can even see the last one trying junk.php before giving up and going to the www.arbucklechurch.org

In olden times the men were made of iron and the ships were made of wood; now it's the other way around. :wink:

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...