Staging Area .htaccess for Adding .php Extensions

September 17, 2015

Tools: .htaccess

Intro

Another .htaccess post...oh the joy.

So I've been doing a lot of site migrations between web hosts lately. This entails uploading files to the new server to allow the client to preview the website before it goes live.

Let's say you're working on a server with an IP of 192.111.2.3. You've uploaded the website to the server and want to preview it. Since the domain hasn't yet been pointed to the new server, you can't visit www.yoursite.com (and what would be the point of previewing it if the domain had already been pointed over?).

Luckily, most web hosts will automatically create a staging area at:


        http://192.111.2.3/~username/
    

Voila. You type that in, there's your site. But if you're like me and truncate .php extensions from your links as you code, you won't be able to view anything but the homepage unless you manually type .php at the end of each filename. This is not good for showing to clients.

So here's the .htaccess file you'll need to automatically add the .php extensions to each link, allowing you and your client to browse as normal:


            RewriteEngine On
            RewriteBase /~username/

            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME}\.php -f
            RewriteRule ^(.+)$ $1.php [L]
    

Note: if you're working on an addon domain, your RewriteBase needs to be:


            RewriteBase /~username/subdomain/