Have .htaccess dynamically get RewriteBase

.htaccessapache-2.2

Depending on where I install my set of files, it seems like RewriteBase seems has to change accordingly if I install it in a subdirectory rather than at the www root.

For my configuration files, I use something like the following to allow for administrators to change the settings to suit their deployment environment.

$yaml = file_exists($yamldir) ? Spyc::YAMLLoad($yamldir) : array();
$default = array(
  'hostname' => 'mydomain.com',
  'base_uri' => '/sub/',
  'mysql_host' => 'localhost',
  'mysql_username' => 'root',
  'mysql_password' => '',
  'schema_name' => 'schema1',
  'table_name' => 'table1'
);

$config = array_merge($default, $yaml);

The config.yml is then SVN or git ignored as to allow for maximum flexibility. It seems like the best way to do it.

Is there an equivalent for the .htaccess file, to read (or better yet, parse) a configuration file that may or may not exist?

Background information

My .htaccess needs are incidentally quite simple in this case:

RewriteEngine On
RewriteBase /sub

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php [L]

It would be probably for the best if I used a Rails-style public directory instead of having an effectively "open" www root, but I can take care of that as needed. Is the RewriteBase even necessary in my case? I'm not sure it is. Even so, I can think of cases in the future where I have a more elaborate .htaccess, where I would be hard-pressed to find a workaround for RewriteBase (or am I wrong in thinking this too?)

Best Answer

You probably should use a custom script for doing the rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap - External Rewriting Program

Related Topic