Basic HTML Coding Building Apache with Mod_Perl and Mod_DSO User Authentication with Apache and Perl/CGI Redirecting URLs with Apache Using Server Side Includes IP Mirror (returns IP address and browser type) HTML Home Page gecko-ak.org home page |
Simple Redirects with ApacheSo, you've built a really cool web site, then learned a new tool--Server Side Includes, or HTML::Mason, for example--and now need to move all of your existing HTML pages from .htm or .html to .shtml or .mhtml...or whatever. You could just move all the pages, but Google already has links to your site, and you've already got millions of readers who have bookmarked your site. How do you update your pages without breaking these bookmarks and hyperlinks? Fortunately, Apache has a solution. There is a simple configuration directive that you can include in either your .htaccess file in the subdirectory you wish to change or in your httpd.conf file called "Redirect". You guessed it--Redirect redirects your web pages to the page of your choice (go figure). Redirect is really easy to use, too. We'll start with the easiest case. Suppose you have a file called index.html that you wish to redirect to index.shtml. The syntax of the redirect command is:
Redirect permanent /index.html /index.shtml
Let's break the command down a little. The "permanent" option tells Apache to send an error message indicating a permanent change in the URL. You can also make temporary changes to the URL (syntax coming soon--I don't have my Apache manual handy at the moment), for example when you are updating a page, and don't want people hitting it while you make changes. Next, is the old URL (the one that is being redirected to the new location) followed by the new URL. The new URL may either by in http://servername/path/newfile.extension format or /path/newfile.extension format; Apache can handle both. You can also omit the file name and extension to redirect an entire directory, like this:
Redirect permanent /Defunct /Replacement
Getting TrickierNow, suppose that you have removed a number of obsolete pages, there are no replacement pages, but you want to redirect the user to a page that contains a friendlier error message...perhaps with your corporate logo and some warm, fuzzy statements. However, there are too many pages to list each one separately, and you don't want to redirect the entire directory because there are still some current pages that you want available. Simply adding a redirect won't work, because either you must list each HTML file separately, or you must redirect an entire directory. However, you can redirect only those pages that are missing by using the ErrorDocument directive like this:
ErrorDocument 404 /FuzzyErrorPage.html
Getting REALLY TrickyNow, suppose that you are working on web pages in a particular directory. You don't want any public access to any of the pages in this directory, but you still want to view them in your web browser. Nothing in either the Redirect or ErrorDocuments directive allows you to limit access to a particular IP or set of IP addresses. However, there is a way to make this work, too...after all, this is the Apache server :) (I really like Apache, if you haven't figured that out yet). The httpd.conf file and your .htaccess files, if enabled, allow you to block access to certain IP addresses or to block access except for certain IP addresses. So, suppose your workstation is located on a LAN using RFC-1918 (private, internal use only) IP space, that you wish to deny access to everyone but your workstation while you update the web pages in a particular directory and that you wish to display a web page explaining that the site is currently down for maintenance. The following snippet of directives in your httpd.conf file or in your .htaccess file will accomplish the task:
Order deny,allow
Deny from all
Allow from 172.16.15.14 (or whatever IP/netblock you wish to allow)
ErrorDocument 403 /Maintenance/projectname.html
The "Deny from all"directive blocks access to your server, the "Allow from..." directive allows access for your workstation, subnet, etc. and the "ErrorDocument 403 /path/file.extension" directive tells Apache to redirect all browsers that receive a "403 Forbidden" error to the URL specified. Where do I put these directives?The Redirect, ErrorDocument, Order/Deny/Allow directives can go either in your httpd.conf file or in the .htaccess file (assuming the appropriate overrides have been granted to the .htaccess file). Personally, I create a Directory entry in my httpd.conf file, since it seems easier to me to keep all of this in one location, rather than scattered about my file system, but it's a matter of personal preference. So, a typical example for me would look like this:
<Directory "/opt/www/htdocs/blah">
Options Indexes MultiViews Includes
AllowOverride AuthConfig
Redirect permanent /MovedFile.html http://www.example.com/WeveMoved.html
ErrorDocument 404 /UnderConstruction.html
</Directory>
I've just scratched the surface of the possibilities. Apache's httpd.conf file and .htaccess files allow powerful manipulation of the web pages that Apached serves--explore the options, and have fun! |
|
© Michael Wallette/gecko-ak.org 2004
All weg pages, documentation, etc. on the gecko-ak.org web site, except where otherwise notated, may be copied, reproduced, etc. for any non-commercial purposes without any additional permission from the author. Please feel free to link to my site, but please do not include my web pages on your site without giving credit to the original source (a readable URL is sufficient), and please do not claim my work as your own. Come on, how lame is it to steal someone else's content, anway? :) |
|