Solved

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

Posted on 2012-12-31
6
225 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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