Soak blog

Get the lowdown on what we love, what we hate and everything in-between.

ronansprake

28.06.2010

By: ronansprake

Under: Technical

PHP pages on 1&1 without the file extension (friendly URLs)

I recently stumbled across an odd problem with 1&1′s mod_rewrite, while enabling friendly URLs for PHP files. Searching for solutions made it apparent this is a very common problem specifically with this host’s packages.

Enabling PHP5 on 1&1 hosting

Add the following line to the .htaccess file in your Web root:

AddType x-mapp-php5 .php

1&1 = 3

The problem only affected PHP pages, or .html pages with PHP contents enabled, where the friendly URL matched the file name. For instance, “someurl.com/monkey” would work for a page called monkey.html, would also work if redirected by mod_rewrite to a page called monkey1.php, but returned a default 404 page if the page was called monkey.php. This FAQ page looked promising, but didn’t solve our problem – there are two solutions, neither one perfect, but acceptable for small sites.

The first is to call your pages something other than the friendly URL, then use your .htaccess file to explicitly redirect each URL to its differently named page, such as:

RewriteEngine on
RewriteRule ^monkey$ /monkey-1.php

The second allows you to keep the file names as they are. The key (as devised by Steve) is to create a directory in the Web root named after each page – these directories can be empty or contain files, it doesn’t matter, they just have to exist.

Hopefully, this explanation will save a few people from tearing their hair out! Let us know if you have any other solutions to this odd problem.

Tags: ,

One Response to “PHP pages on 1&1 without the file extension (friendly URLs)”

  1. Michael says:

    Hi,

    if you place your target php file in a subdir you can keep the original file names.

    Like this:
    RewriteRule ^monkey$ somename/monkey.php

Leave a Reply