Preparing the Server
YES DARLING, it’s a SERVER!
Not really, though. Just another virtual host. But it will do my low-requirement job, that for sure. I had to change my hosting provider because the ZEND-framework requires PHP > 5.1.4 and my old provider, who was great on everything else, didn’t want to upgrade. So on to the big discounters, and there you go – hostmonster.com got a new customer. Support has proven to be highest quality, fast and friendly, the product works, the features are great. I’m all in.
Directory structure
But with a new server there comes a change. And when something changes in my life there has to be an improvement. Changing to the better, that’s the way. So the new server just was craving for a wisely planned, logical directory structure, taking in account possible multiple ZEND applications that are to be developed in the future. So what’s that directory structure all about? Let’s have a look with my approach of law and order:
~home
+- /applications
| +- /appname
| |- config.xml
| +- /controllers
| +- /languages
| +- /models
| +- /views
+- /php_include_dir
| +- /Extensions
| +- /Smarty
| +- /Zend
+- /public_html
|- .htaccess
|- errorpage.php
+- /apps
+- /appname
+- /dev <-- dev.kno.at
+- /appname
+- /files
+- /sites
+- /sitename
+- /blog-wordpress <-- blog.kno.at
+- /cheap <-- cheap.kno.at
Nothing really special, except for the fact that the
/applications
-directory is beyoung the www-root. This is for security reasons, this way no one will ever be able to access those files, even if he exactly knows where they are and what they are. I like security. The php_include_dir is added to the include_path in php.ini and the directories are organized in a way that the ZEND AutoLoader can easily find all those files. This keeps the application-files slim by limiting include-statements to one: include 'Zend/Loader.php';
Subdomains
In the directory structure you also see where the subdomains point to. In the first place I directly assigned them to those directories. This worked just fine, until I tried to come up with a custom error document wrapper that should also cover the documents on those subdomains. The problem was that the server was configured to assign the folder the subdomain points to as this subdomains docroot and therefore didn’t mind everything above it when accessing files there. So I set up these three subdomains to point to the global docroot and then redirected them using the apache rewrite module in the global .htaccess file. This looks like so:
# test.domain.com > /my/demo/folder
RewriteCond %{HTTP_HOST} ^(www.)?test.domain.com$ # has test.domain.com been accessed?
RewriteCond %{REQUEST_URI} !^/my/demo/folder/ # don't redirect dir-access
RewriteCond %{REQUEST_FILENAME} !-f # don't redirect ex. files
RewriteCond %{REQUEST_FILENAME} !-d # don't redirect ex. dirs
RewriteRule ^(.*)$ /my/demo/folder/$1 # rewrite to subfolder
RewriteCond %{HTTP_HOST} ^(www.)?test.domain.com$ # subdomain root access
RewriteRule ^(/)?$ my/demo/folder/ [L] # rewrite, no more rules
Custom Error Pages
As I had this subdomain-handling on the go I could focus on the error-page wrapper. To keep things native I used the default
ErrorDocument
directives for the common errors (400, 401, 403, 404, 500) plus a following rule to catch other errors (starting with 4 or 5, that is), resulting in the code:
# custom error pages
ErrorDocument 400 /errorpage.php?sc=400
RewriteCond %{REQUEST_URI} !-d # don't rewrite existing directories
RewriteCond %{REQUEST_URI} !-f # don't rewrite existing files
RewriteRule ^([4-5][0-9]{2})\.(.*)$ errorpage.php?sc=$1 # map 3-digit-files to errordoc
Within the error-document-wrapper (errorpage.php that is) the corresponding headers are sent using the PHP-function header(); and an error document outruling the default humpty-tumpty files is being displayed. One major point in doing so was not just to show a page that says sorry, nothing for you, but to take this user by the hand and say: you didn’t find what you tried to, but why not try this or that? and keeping him on the site, rather than blaming him for someone else’s fault (most likely he got here from an external link, or because I deleted a file).