Mods: Automatic Base Detection
Summary
In the most applications that are using redirections and rewrites you have to modify at least the "RewriteBase" if you move them from one subfolder into another. Some frameworks make our life unnecessarily difficult changing the domain name and some other need extra code to generate relative Url's.
With CodeIgniter for example you have a function "base_url()" which helps you to built links to ressources your application without to know about your current place. But this not very handy as you have not forget to use this function and the code looks very ugly:
In an <a href>-Tag:
<a href="<?php echo base_url();?>controller/method/parameter">...</a>
Or in a JSON-Object:
{
url: "<?php echo base_url();?>controller/method/parameter"
}
CodeCoupler includes a htaccess file redirecting by default everything into the "index.php". Furthermore this htaccess file detects the current base url automatically and save it in a server variable. Then CodeCoupler takes this base url and use it in every page in the base-tag.
<base href="<?=$_SERVER['BASE']?>">
In addition to this CodeIgniter removed the value "index.php" from the variable "index_page" in "config.php". So you do not have to bother from now how to point to ressources anymore. Just start everytime with the name of controller:
In an <a href>-Tag:
<a href="controller/method/parameter">...</a>
Or in a JSON-Object:
{
url: "controller/method/parameter"
}
A note to the "base_url" variable in "config.php" of CodeIgniter: This variable should always be empty to let CodeIgniter automatically detect the protocol the hostname and the subdirectory in functions like "base_url()". In some cases you have to set this variable and then you have another place to remind if you move your application around. With CodeCoupler you can set this variable in a way that moving the application at least to another folder do not break your links:
$config['base_url'] = 'http://YOUR_FIXED_DOMAIN/'.(is_cli()?'':ltrim($_SERVER['BASE'],'/'));
CodeCoupler adopted the solution published by Jon Lin in a Stack Overflow Answer from here: http://stackoverflow.com/a/21027742
How this works
Every line of the htaccess file explained:
RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]The first line looks if the variable URI is empty. If yes, the second line stores the path from the location of the htaccess file to the requested file into the variable "URI".
- Example Request: http://host/subdir/request-dir/request-file?var=val
- If this htaccess is placed in "subdir" the stored value will be: request-dir/request-file
RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]The first line looks if the variable BASE is empty. The condition of the second line is always true. The only point of this line is saving the some values in two groups. It works as follows:
Our example Request: http://host/subdir/request-dir/request-file?var=val
The variable URI have the value because of the rule above: request-dir/request-file
Now the condition is:
Test-String: request-dir/request-file::/subdir/request-dir/request-file
Condition : ( group 1 )::(group2)request-dir/request-file
^- This is the -^
^- Backreference \1 -^
^- to the value of -^
^- "group 1" -^Now we have in "group 2" the base that we need. The third line save this value into the variable BASE. And we are ready for our final rewrite:
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ %{ENV:BASE}index.php [L,QSA]If you need to exclude some requests redirecting to "index.php" you can add them in the rewrite condition like here:
RewriteCond $1 !^(index\.php|assets)