Inconsistency when switching between development to live server?

I always have trouble when moving a site that I've developed in localhost into my hosting server with the .htaccess file. Specifically with relative paths to the files. It's not hard to fix, but it's quite bothering.

For example:

  • Whereas ErrorDocument 404 /foo/404.php works in the hosting, for it to work in localhost I have to specify ErrorDocument 404 /projectroot/foo/404.php.

  • When using mod_rewrite, RewriteRule ^example/$ example.php [L,NC] works in localhost, but for it to work in the hosting I must use RewriteRule ^example/$ /example.php [L,NC] (note the slash before the file name).

It could be because I'm running localhost on Windows and my hosting is on Linux, but I don't think that's the problem.

So my question is, how can I ensure the paths to the files are correct when working locally or on the remote server? What is the "working directory" for the .htaccess file?


Solution 1:

This may look slight more complicated then it seems, but I would work out your issue in the following way.

  1. Create a dev domain on the C:\Windows\System32\drivers\etc\hosts file pointing to the IP 127.0.0.1 which is the localhost IP.

    The domain name doesn't really matter just something easy for you to use for that given project for example test.dev so the hosts file would look like this:

    # localhost name resolution is handled within DNS itself.
    #   127.0.0.1       localhost
    #   ::1             localhost
    127.0.0.1   localhost
    127.0.0.1   test.dev
    127.0.0.1   www.test.dev
    
  2. Now define where the folder for that project will be, I will use c:\projects\test for this example.

  3. Now create on your web server a new virtualhost for the domain you have just created, here is a sample of virtualhost that I use:

    <VirtualHost *:80>
            ServerAdmin root@localhost
            DocumentRoot "C:/projects/test"
            ServerName test.dev
            ServerAlias www.test.dev
            ErrorLog "C:/projects/logs/test.dev_error_log"
            CustomLog "C:/projects/logs/test.dev_access_log" common
            RewriteLog "C:/projects/logs/test.dev_rewrite_log"
            RewriteLogLevel 9
            ScriptAlias /cgi-bin/ "C:/projects/test/cgi-bin/"
    
            <Directory "C:/projects/test">
                    Options -Indexes FollowSymLinks +ExecCGI
                    AllowOverride All
                    Order allow,deny
                    Allow from all
            </Directory>
            <Directory "C:/projects/test/cgi-bin">
                    AllowOverride None
                    Options None
                    Order allow,deny
                    Allow from all
            </Directory>
    </VirtualHost>
    
  4. Restart your server to activate the changes, you do not need to restart the computer for the hosts file, it should work immediately once you save it.

  5. Now you can use http://www.test.dev or http://test.dev and you can use the very same .htaccess on both, dev and live sites.


Some other tips:

  1. Always use and define RewriteBase / as it can make a difference specially when you want to use rules without the / at the start, like you have mentioned for RewriteRule ^example/$ example.php [L,NC]

  2. Depending on the rules you want to use, you may need to specify -MultiViews so I often just leave it on by default, like this:

    Options +FollowSymLinks -MultiViews
    
  3. As you can see on my virtualhost setup, I define the access, error and rewrite logs into the logs folder by default and with the domain name in question so when I need to see what is going on I have an easy access with the clear logs for that domain only and not my whole bunch of projects running on the same virualhost.

I guess that is about it, you don't really need that huge virtualhost that's just how I use it and its not like I need to copy and paste it all the time as I have my own simple script to create new domains and do all the work for me.