htaccess work-arounds for common 1and1 (1&1) problems

make all *.php files use PHP5

Problem: 1and1 by defaults requires you to name your php5 with a *.php5 extension. by default *.php files are handled by PHP4 engine. eg; to use php5 in your index page, you'd be required to name the file index.php5

Workaround: via htaccess you can force the default *.php files to be handled by the php5 engine.

Code to add to htaccess:

AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php 


using/formatting "php source" files

Problem: *.phps files (and the associated formatting with those files) are turned off for 1and1. However, the PHP function highlight_file() does work. It is just the entire content-type / mime-type doesn't exist for phps files. A new content/mime type can not be added there either. Example of what *should* work and what I would normally add:

# DOES NOT WORK ON 1and1
AddType application/x-httpd-php-source .phps
AddHandler application/x-httpd-php-source .phps

However, that does not work on 1and1. So the work-around is two part.

Workaround Part 1: Create and save a PHP file that uses highlight_file() to parse the file contents instead.

<?php
$uri=$_SERVER['DOCUMENT_ROOT'].str_replace("../","",$_SERVER['REQUEST_URI']);

if (file_exists($uri))
	highlight_file($uri);
else
	echo "PHPS File Not Found";
?>

Workaround Part 2: Forward all *.phps requests through the above script. Add this to your htaccess (make sure you change the path /scripts/setup/phps.php to point to the path/filename you saved your script to).

RewriteEngine on
RewriteRule (.*)\.phps$ /scripts/setup/phps.php [L]


make all 404 errors work correctly on php files

Problem: Custom 404 (File Not Found) errors for php files on 1and1 hosted sites don't work with a normal ErrorDocument. 1&1 is bound and determined to show their own error messages (with their own advertising on it of course). This is an example of what *should* work and what I would normally add:

# DOES NOT WORK RELIABLY ON 1and1 FOR PHP FILES
ErrorDocument 404 /404.php

Workaround: via htaccess, instead of using ErrorDocument to catch the error, you can use RewriteCond to redirect any erroring pages.

Code to add to htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /404.php

If you still incounter general weirdness, try renaming the 404 error page to error404.html because for some reason 1and1 look for error files named that first.

Comments

Run these two scripts via backtick from a PHP page within the directories you want to convert.

- Bulk renames old PHP files to php5
`find . | sed -n 's:\(.*\)\.php:mv "\1.php" "\1.php5":p' | sh`

- Replaces all content to point to the new files
`perl -pi -w -e 's/\.cfm/\.php5/g;' *.php`

-Ed
www.edwardprevost.info