Is it possible to have a single .htaccess file for both localhost and online

Hi All,

Unfortunately I have Googled and not been able to find a solution so…

Problem:
Two different .htaccess files required for http://localhost and http://MySite.com
Having to maintain two different .htaccess files with the same name.
Try to prevent renaming and uploading incorrect .htaccess files

Requirements:
I would prefer a single .htaccess file that detects HOST and performs relevant script

Reason:
I struggle with Apache’s htaccess and have a hard job in trying to find the relevant
information in the wealth of free advice :frowning:

Solution:
With PHP this is a simple task:

PHP eqivalent to Apache .htaccess file



#test host
  defined('LOCALHOST') ? NULL : define('LOCALHOST', in_array($_SERVER['SERVER_NAME'], array('localhost', '127.0.0.1') ));

# condition for specific host
  if( LOCALHOST )
  {
    include 'htaccess-for-localhost.php'; 
   # Read somewhere that not availalble in Apache
  }
  else # if( ! LOCALHOST )
  {
    include 'htaccess-for-MySite.php'; 
  }
  
#Common stuff
  ErrorDocument 404 /404.php  
    

John,

That’s exactly what the %{HTTP_HOST} variable does for you (unless you’ve been silly enough to name your localhost domain the same as the production server domain).

Regards,

DK

@dklynn

Hi David,

I do not think I was very clear. The PHP script I supplied was to show what I want with the .htaccess file.

I still do not understand how to:

  1. test for %{HTTP_HOST} in the .htaccess file

  2. include files in the .htaccess file

I have not renamed localhost.

John,

Neither mod_rewrite nor Apache in general “reads” a PHP file (a program is called - PHP - which parses .php files to create output which is then served by Apache). In other words, your PHP script is useless (because it is long after the fact).

RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
...
{your RewriteRules ... watch out as they're ANDed with the first RewriteCond}

Note: This assumes that you’re not using local VirtualHost “domains” to connect to localhost.

If you understand mod_rewrite a little more, you can use the Skip flag to count the number or RewriteRules to skip over IF the RewriteCond is true (and associated RewriteRule is matched) … it works like an if … then structure but requires good counting. Catch more on that in my signature’s tutorial.

Regards,

DK

@dklynn

Hi David,

I have managed to solve my problem…

I created a test-bed, online, sub-domain that was not associated with the CDN Cache. A simple PHP webpage with a .htaccess file was used and I was able to experiment and not panic. I do not enjoy having to firefight/resurrect the site (with generated cache) in order to… “Where would you like to go today”:slight_smile:

Testing revealed the conflict was due to <ifModule mod_expires.c>. Localhost threw a wobbler if the statement block did not surround ExpiresActive on, etc

Mission accomplished and now have a single .htaccess file that I can use safely for the Localhost and Online :slight_smile:

I did look at your comprehensive online Code Generator and associated links but was unable to find a solution to my problem. Maybe you could add a section on testing or surrounding new code in switchable blocks to facilitate .htaccess upgrades.

Many thanks for your help,

Cheers,

John

Hi John,

In the tutorial article’s section labeled Flags:

IMHO, that’s a bit much for the attempt to teach via a code generator so I think I’ll just leave it in the tutorial.

'Glad you got your problem solved.

Regards,

DK

Hi David,

Many thanks for the example.

In the Apache script language I was trying to find a method of testing for the environment (localhost) and on success do a block else do another block. Every language I have learnt has had this option except TRS80 Basic, where a rigid amount of lines had to be skipped. This Basic language fell by the wayside and was superseded with procedural blocks.

Yes I am pleased to have solved the problem and now onto the next hurdle…

Cheers,

John