Solved

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

Posted on 2012-12-31
6
223 Views
Last Modified: 2013-01-05
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
Comment
Question by:Kenbg
  • 2
  • 2
  • 2
6 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 38734591
mod_rewrite is your friend

a simple rule there might look like:

RewriteCond %{REQUEST_URI}  \.shtml$
RewriteRule (.*)\.shtml $1.php
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 38734966
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
 

Author Comment

by:Kenbg
ID: 38736732
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 250 total points
ID: 38737834
> .. 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
 
LVL 50

Accepted Solution

by:
Steve Bink earned 250 total points
ID: 38738391
        # 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
 

Author Closing Comment

by:Kenbg
ID: 38747374
I think that with both of these ideas I can work it out.
Thanks
Ken
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now