?
Solved

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

Posted on 2012-12-31
6
Medium Priority
?
229 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
[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
  • 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 51

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 1000 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 51

Accepted Solution

by:
Steve Bink earned 1000 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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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