?
Solved

Build a catchstring to redirect pages

Posted on 2011-03-02
10
Medium Priority
?
246 Views
Last Modified: 2012-05-11
I have a site that has lost some pages. I am trying to catch the strings in old url from search engines and redirict them to the new replacement page. I am not sure if I am going the right direction with my code. And I have a provlem with url's that are a directory deep.

Here is what I am using:

function stripstuff( $string )
{
  $string = str_replace ( '/', '', $string );
  $string = split ( '[?]', $string );
  strtolower(trim($string[0]));
  return $string[0];
}

    $theFile = $_SERVER["REQUEST_URI"];
     if ($theFile == "/" || $theFile == "") 
    {
        // If nothing redirect to home page
        header('Location: index.php'); 
    } 
    else 
    {
        $theFile2 = stripstuff($theFile);
        query database for matching string
        if ($num_rows > '0')
        {
            while ($rowID = mysql_fetch_array($result))
            {  
                $ArticleCkID = $rowID['ArticleID'];
            } 
        }
        else
        {
            header('Location: site-map.php'); 
        }

Open in new window


I have 3 questions/issues:
1. Is this the best way to achieve this?
2. When there is a sub-folder (www.domain.com/articles/this-is-the-file.php) the catch string has to be "articlesthis-is-the-file.php" for it to find the proper file
and
3. It doesn't show images bacause it is not in the proper folder level.
0
Comment
Question by:Jeff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35022661
Have you got this in a 404 handler script?

I think I would do it by having a table of relationships - old URL to new URL.

0
 

Author Comment

by:Jeff
ID: 35022847
Thanks Ray,
Yes and Yes. That is exactly what I am doing.
The problem I am having is with the "/". When I remove it ($string = str_replace ( '/', '', $string );
) I am assuming the file to look for will be www.domain.com/file.php. It strips it to file.php. However if it is www.domain.com/folder/file.php it strips it to "folderfile.php". It then is looking for the images from the "www.domain.com/folder/" folder level and no images show.
0
 

Author Comment

by:Jeff
ID: 35022861
As well as looking for "folderfile.php" that doesn't exist.
0
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35022869
OK, let me ask the obvious question... Why are you removing the directory separators?  Why not just translate $_SERVER["REQUEST_URI"] directly?
0
 

Author Comment

by:Jeff
ID: 35022952
$_SERVER["REQUEST_URI"]  grabs everything after the .com. "/file.php" or "/folder/file.php".


	function stripstuff( $string )
	{
	  $string = str_replace ( '/', '', $string );
	  $string = split ( '[?]', $string );
	  strtolower(trim($string[0]));
	  return $string[0];
	}

	$theFile = $_SERVER["REQUEST_URI"];
if ($debug) { echo "URI = " . $_SERVER["REQUEST_URI"] . "<br />"; }
	if ($theFile == "/" || $theFile == "") 

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35022997
Yes, exactly.  It contains PHP_SELF plus the query string.  Maybe I am not understanding how your site has been re-organized, but it would seem to me that you could use PHP_SELF or REQUEST_URI to perform a lookup of the new URL, then redirect.  Here is the code for one of my catch-all 404 handlers.  It lets you have both of these URLs wind up in the same place"

example.com/script
example.com/script.php

Not very sophisticated but it will work for these, too.

example.com/script/foo
example.com/script/foo.php
<?php // 404handler.php
error_reporting(E_ALL);

if (isset($_SERVER["REQUEST_URI"]))
{
    // IF A PHP SCRIPT IS NOT FOUND GO TO THE HOME PAGE
    if (preg_match('#\.php#i', $_SERVER["REQUEST_URI"]))
    {
        header('Location: /');
        exit;
    }

    // IF A NON-PHP SCRIPT IS NOT FOUND, TRY IT WITH PHP
    $arr = explode('?', $_SERVER["REQUEST_URI"]);
    $arr[0] .= '.php';
    $uri = implode('?', $arr);
    header("Location: $uri");
    exit;
}

// IF NO REQUEST URI
header('Location: /');
exit;

Open in new window

0
 

Author Comment

by:Jeff
ID: 35035491
Thanks Ray, I will use some ot that code.

I still have one problem,

When grabbing the url, a folder in between makes it hard to do a search.
Example:
$_SERVER["REQUEST_URI"] for www.somain.com/foldername/filename.php returns "/foldername/filename.php".

How would I trim this to just "filename.php" so I can search to see if that page exists in my database?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35036838
You can use basename()
http://us2.php.net/manual/en/function.basename.php

But you might consider the design pattern of something like this:
www.domain.com/tom/index.php
www.domain.com/ray/index.php

Tom and Ray might not have exactly the same index.php -- just a thought.

0
 

Author Comment

by:Jeff
ID: 35038886
What I have is a site that was shut down for about a week. I can look up on google and view the cached pages. Problem is they are shtml. So I am building a catchstring script. I view the cached page, build a new page. Some pages are in the main directory and some are in sub directories. I am trying to strip the subdirectiory then redirect to a page created from the cache.

Does that help to make sense?
0
 

Author Comment

by:Jeff
ID: 35039067
basename() works perfect!

Thanks Ray.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question