[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Help with new site design, using htaccess to avoid 404 pages

Hopefully someone can give me a few tips and tricks.  I have a large site, approx 4000 pages, which half static and half dynamic .  They have names like XXX.shtml because they use server side includes for data.  
My problem is, I have redesigned the site to use PHP and most of the pages are dynamic, I don't want to have people clicking on a google link and getting a 404 error because the old page won't exist anymore.  

I know that eventually google with switch to the new pages but what do I do in the meantime?
Should I use a redirect or a rename?  Is there some kind of global statement that I can use to avoid having to put in commands for 4000 pages?

Will a redirect work if the page being redirected from no longer exists?

I have played with .htaccess  a little bit but it seems to be black magic.

Thanks for the help
Ken Graser
0
Ken Graser
Asked:
Ken Graser
  • 2
  • 2
  • 2
2 Solutions
 
ahoffmannCommented:
mod_rewrite is your friend

a simple rule there might look like:

RewriteCond %{REQUEST_URI}  \.shtml$
RewriteRule (.*)\.shtml $1.php
0
 
Steve BinkCommented:
Addition: mod_rewrite is your *best* friend.

A simple rule for redirecting all potential 404's to a handling page:

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.+) /My_Hander_Page?original=$1 [QSA,NS]

From the handler page, you can either return the content you want to show, redirect them to the page you think they wanted, or send a custom 404 page.
0
 
Ken GraserIT ManagerAuthor Commented:
I have a few questions.

ahoffmann - does that simple rule require that the file names are the same ie: widget.shtml  goes to widget.php?  All my file names may or may not be the same so how would this work?

routinet - This sounds good but how do I set up the handling page?  Could you give me a brief explanation of the rule.?

I think we are on the right track so if I can just get my brain wrapped around this I'll be good.

Thanks
Ken
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ahoffmannCommented:
> .. require that the file names are the same ..
yes

> .. my file names may or may not be the same
see routinet's solution
or mod_rewrite also has mode where all mappings can be read from a file (best in db format)
0
 
Steve BinkCommented:
        # if the request does not match a valid, resolved file
        RewriteCond %{REQUEST_FILENAME} !-f
        # and the request does not match a valid, resolved directory
        RewriteCond %{REQUEST_FILENAME} !-d
        # then take everything after the initial slash, and store it in $1  --> /?(.*)
        # rewrite the URL to server My_Handler_Page, using $1 as a query string parameter
        # and append the original query string, if any
        RewriteRule /?(.*) /My_Handler_Page?original=$1 [QSA]

Open in new window


Note that I edited the directives slightly, since my initial post was a bit loose.

To set up the handling page, just make a new .php file.  Assuming the rule above, ensure  the "/My_Handler_Page" part is the actual path/filename of the handler file you create.  The handler code could look something this:

<?
$request='';
if (isset($_GET['original'])) { $request=$_GET['original']; }
if (!$request) {
  // there is no "original" parameter passed...do something cool and intuitive
} else {
  // here, you can use $_SERVER, $_GET, etc., to determine what you actually want to do
  switch ($request) {
     case 'this_page.shtml': header('location:this_new_page.php'); break;
     default:
         // create your default "not found" page here
         break;
  }
}

Open in new window


Admittedly, that example is pretty basic.  Since you have full access to the entire context of the request, including cookies and any related session information you might have in a database, you could really do anything you wanted in the handler.  If you're just looking for a basic 1-to-1 map of "this-old-url" to "this-new-url", you should look at the solution ahoffmann referenced in his last post.

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap

Don't worry, it is must easier than it sounds.  This is a bit beefier than a normal rule set, and it is a bit slower.  You probably would not notice unless you're handling a few hundred requests a second.  This method allows you to just keep a managed list of the 1-to-1 matched redirects.  Once it is set up, you only need to change the list, not write any new rules.
0
 
Ken GraserIT ManagerAuthor Commented:
I think that with both of these ideas I can work it out.
Thanks
Ken
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now