SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)

    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

    Solution:
    With PHP this is a simple task:

    # PHP eqivalent to Apache .htaccess file
    PHP Code:

    #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 
    Last edited by John_Betong; Mar 14, 2012 at 22:17. Reason: formatting comments
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @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.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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).
    Code:
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @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"

    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

    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
    Last edited by John_Betong; Mar 18, 2012 at 08:35. Reason: spelling: not my fortey
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi John,

    In the tutorial article's section labeled Flags:
    Quote Originally Posted by http://dk.co.nz/seo
    {snip}
    skip|S=n
    The Skip ([S]) tells mod_rewrite that want to skip the next n RewriteRules. This is useful as it acts like an if...then...else OR GOTO in the mod_rewrite code.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} example\.com$ [NC]
    RewriteRule .? - [S=2]

    RewriteRule ^subdir1/(.*)$ subdir2/$1 [L]
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^index\.php$ index.php?marker=on [L]

    // the first RewriteCond/RewriteRule act as the if and then
    // skipping the else of the next two RewriteRules
    // (and their associated RewriteCond statements)
    // These can be followed by other mod_rewrite statements.
    {snip}
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    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
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •