Solved

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

Posted on 2012-12-31
6
224 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
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
apache and php 3 99
file path 14 67
How to find Tomcat initial memory and Max memory through command line windows 12 147
AWS Advice on using WHM/cPanel 1 69
This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 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

17 Experts available now in Live!

Get 1:1 Help Now