Aamir Siddique
Aamir Siddique

Reputation: 334

Can we change include_once path using .htaccess?

I have a copy of a site on a live server and a local server. The files have so many include paths in it, which are like this:

require_once "/long/path/on/server/to/the/site/functions.php";

Local server has this and other similar files located in the same directory

Now instead of changing all these paths on the files i am thinking of using .htaccess instead. This way I will keep the htaccess file on local only and wouldn't have to change paths all the time when uploading to live server.

So any ideas on this?

Upvotes: 0

Views: 370

Answers (4)

Jaspreet Chahal
Jaspreet Chahal

Reputation: 2750

Create symbolic links. That's makes it clean and easy. And can be easily integrated in your project.

For *nix you can use:

ln -s /path/to/original/ /path/to/linkName

For Windows (if the filesystem is NTFS) you can use:

mklink [/D] drive:/path/to/orignal drive:/path/to/linkName

Upvotes: 2

Your Common Sense
Your Common Sense

Reputation: 157918

Usually you don't even need to touch web-server configuration for this, as such a variable already defined in the properly configured server by default.

require_once $_SERVER['DOCUMENT_ROOT']."/functions.php";

will load proper file in any environment.

Nevertheless, you can set whatever environment variable using SetEnv directive and read it from $_SERVER array in the PHP script.

However, you aren't supposed to use this variable all throughout your scripts.
Better practice would be to use it to create a constant which would be used later on.
You can mace creation of this constant conditional, depends on the environment.

For the systems with single entry point things become even more easier: you may have the root path built out of the entry point script address.

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72682

To make you life easier why don't you implement an autoloader:

class AutoLoader
{
    public function loadLibFile($class)
    {
        $filename = '/long/path/on/server/to/the/site/' . $class . '.php';

        if (!is_readable($filename)) {
            return false;
        }

        include($filename);
    }
}

$autoloader = new AutoLoader();

spl_autoload_register(array($autoloader, 'loadLibFile'));

Upvotes: 0

Vytautas
Vytautas

Reputation: 3539

You can define constant for path:

define('ROOT_PATH', '/long/path/on/server/to/the/site/');
require_once ROOT_PATH . "functions.php";

or you can change include paths

set_include_path('/long/path/on/server/to/the/site/');
require_once "functions.php";

Upvotes: 0

Related Questions