Category: "LAMP"

ErrorDocument * index.php

Using a site’s home page as an error document is a good way to prevent people and ‘bots from learning more about the site architecture. Routing all error requests to the home page, or some other page, will reduce information leaks, such as ‘file not found’, ‘access denied’, and script failures which could display error information.

It is important to monitor the server error log to see what errors are occurring. Be sure there is a robots.txt file, even if it is empty. Check the log and stats to see what files people are requesting.

Be careful not to confuse legitimate users - if you remove pages or update content frequently, you may want to use 301 redirects for those pages to alert site visitors and search engines of the change.

Wicked Good Gallery - Lightweight LAMP, Easy to Administer

The Wicked Good Gallery grew from an idea mentioned at http://w3schools.com/forum and discussions with the people at http://lilyclaireart.com.

The challenge was to build a gallery application that would allow site visitors to view and purchase artwork and be very easy to administer.

The gallery offers two viewing modes, navigation through the thumbnails at the top of the page, and a slide show. The thumbnails are limited by the width of the page, so not all thumbnails may be displayed. Arrows are used to indicate additional images earlier or later in the list. Clicking on a thumbnail displays a larger image, along with the associated HTML. The slide show hides the HTML and centers the image, then loops through the images to display all the images of the gallery. It’s as if the site visitor is walking through the gallery, looking at artwork.

Administration is through SFTP/FTP/SCP. The person running the site creates a directory, named for the image, and places the original image .jpg in it, as well as free form HTML. When the site loads the next time, it will find the new directory, check for a thumbnail and detail image, and create them if necessary. When site visitors click on the thumbnail, the HTML will be displayed with the image.

With respect to SEO, each image is represented with a dedicated URL. The artwork’s name is used as the title, and the more text in the HTML for that image, the better.

All requests into the application are routed through a single file. If the requests map to an image, the appropriate artwork is displayed, otherwise, the home page, or contact page is shown.

Real-Time Apache Server Monitor

Apache includes mod_status, which lets you monitor the server status, in real-time. Click the link above to see it running up at http://apache.org.

There are also ways to automate the reporting, so you can gain further insight into performance, and you should be aware that there may be overhead related to this, but, it’s a great diagnostic tool.

All the configuration is already in /etc/httpd/conf/httpd.conf.

Uncomment the line to enable ExtendedStatus, then the lines referring to server-status, the Location tag and all its contents. Finally, enter your IP address in the Allow directive.

Restart Apache.

Access the status with domain.com/server-status. You can add the refresh= option to auto refresh the display.

And, it’s free.

Mobile Device HTML Page - Detect and Redirect

First, the device must be identified so the correct content can be served.

An .htaccess file can be used, or these settings can be placed in httpd.conf (or an included .conf file). This is an Apache 1.3 version, 2.+ may be slightly different.


# Set the MIME type
AddType "application/xhtml+xml;charset=utf-8" .html

# This handles the redirection
RewriteEngine On

# Don't redirect requests for images
RewriteRule \.(gif|jpe?g|png)$ - [L]

# Test for the user agent.  Mozilla is used to indicate a non-mobile device
# A more complex RewriteRule would be required for a production environment
# http://en.wikipedia.org/wiki/List_of_user_agents_for_mobile_phones is a good list
RewriteCond %{HTTP_USER_AGENT} !^Mozilla.*

# Use this page for mobile devices
RewriteRule .* wap.html       [L]

# Otherwise process the request normally

In this case, the page is a very simple page, to let people know that they need to use a browser to visit the page.



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
    "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type"
                content="application/xhtml+xml;charset=utf-8" />
<meta http-equiv="Cache-Control" content="max-age=86400" />
<title>site.com</title>
<style type="text/css">
body
{
font-family:verdana,arial,sans-serif;
text-align:center;
margin:0 auto;
}
</style>
</head>
<body>
  <h1>Welcome to my.site.com</h1>
  <p><img src="icon64x64.gif" alt="logo" title="logo" height="64" width="64" /></p>
  <p>Please visit my.site.com from a laptop or desktop.</p>
</body>
</html>

Validate the code using the link above to ensure it displays well in most devices.

A different approach, using a .jsp is at: https://core.forge.funambol.org/wiki/HowToMakeAMobileOKPageForThePortal, a similar script could be adapted for PHP and ASP.

Using .htaccess to Report Delivery Time

The question was whether the number of rewrite rules in .htaccess would have a significant impact on performance.

With the following set of rewrite rules:

Options +FollowSymLinks -Indexes
<IfModule mod_php5.c>
        php_value magic_quotes_gpc 0
        php_value magic_quotes_runtime 0
        php_value allow_call_time_pass_reference 0
</IfModule>
DirectoryIndex index.php
<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^cms/index\.php/plain_admin$ http://domain.com/admin [R=3
01,L]
        RewriteRule ^cms/index\.php/plain(.*) $1 [R=301,L]
        RewriteRule ^media.* - [L]
        RewriteRule ^custom.* - [L]
        RewriteRule content/treemenu/? /index_treemenu.php [L]
        RewriteRule ^var/[^/]+/cache/public/.* - [L]
        RewriteRule ^var/storage/.* - [L]
        RewriteRule ^var/[^/]+/storage/.* - [L]
        RewriteRule ^var/cache/texttoimage/.* - [L]
        RewriteRule ^var/[^/]+/cache/texttoimage/.* - [L]
        RewriteRule ^design/[^/]+/(stylesheets|images|javascript)/.* - [L]
        RewriteRule ^share/icons/.* - [L]
        RewriteRule ^extension/[^/]+/design/[^/]+/(stylesheets|images|javascripts?)/.* - [L]
        RewriteRule ^packages/styles/.+/(stylesheets|images|javascript)/[^/]+/.* - [L]
        RewriteRule ^packages/styles/.+/thumbnail/.* - [L]
        RewriteRule ^/favicon\.ico - [L]
        RewriteRule ^/robots\.txt - [L]
        # Uncomment the following lines when using popup style debug.
        # RewriteRule ^/var/cache/debug\.html.* - [L]
        # RewriteRule ^/var/[^/]+/cache/debug\.html.* - [L]

        RewriteCond %{HTTP_HOST} ^webdav\..*
        RewriteRule ^(.*) /webdav.php [L]

        RewriteRule .* index.php
</IfModule>

The results were:

Received: t=1267660375832889 Duration: D=273652
Received: t=1267660761803171 Duration: D=297682
Received: t=1267660779066518 Duration: D=272959
Received: t=1267660804103095 Duration: D=292675
Received: t=1267660821137470 Duration: D=256268

This set of rewrite rules:

Options +FollowSymLinks -Indexes
<IfModule mod_php5.c>
        php_value magic_quotes_gpc 0
        php_value magic_quotes_runtime 0
        php_value allow_call_time_pass_reference 0
</IfModule>
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^media.* - [L]
RewriteRule ^custom.* - [L]
RewriteRule !(\.(gif|jpe?g?|png|css|swf|mp4|js|html?)|var(.+)storage.pdf(.+)\.pdf)$ index.php
</IfModule>

Yielded these results:

Received: t=1267660624947466 Duration: D=279933
Received: t=1267660660237894 Duration: D=289207
Received: t=1267660681578539 Duration: D=327192
Received: t=1267660701894745 Duration: D=254252
Received: t=1267660719493366 Duration: D=262131

This was a very informal test, just to see if there were any glaring differences, as you can see, the results were reasonably close.