Mods: Automatic Base Detection: Unterschied zwischen den Versionen

Aus CodeCoupler Wiki
Zur Navigation springen Zur Suche springen
Tm (Diskussion | Beiträge)
Tm (Diskussion | Beiträge)
Zeile 1: Zeile 1:
== Summary ==
== Summary ==
=== The Problem ===


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.
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.
Zeile 18: Zeile 16:
}
}
</syntaxhighlight>
</syntaxhighlight>
=== The Solution ===


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.  
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.  
Zeile 28: Zeile 24:


In addition to this CodeIgniter removed the value "index.php" from the variable "index_page" in "config.php" for clean straight URL's
In addition to this CodeIgniter removed the value "index.php" from the variable "index_page" in "config.php" for clean straight URL's
=== The Result ===


From now you do not have to bother how to point to ressources anymore. Just start everytime with the name of controller:
From now you do not have to bother how to point to ressources anymore. Just start everytime with the name of controller:
Zeile 45: Zeile 39:
</syntaxhighlight>
</syntaxhighlight>


=== Some Notes ===
== Some Notes ==


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:
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:
Zeile 53: Zeile 47:
</syntaxhighlight>
</syntaxhighlight>


=== Thanks ===
== Thanks ==


CodeCoupler adopted the solution published by Jon Lin in a Stack Overflow Answer from here: http://stackoverflow.com/a/21027742
CodeCoupler adopted the solution published by Jon Lin in a Stack Overflow Answer from here: http://stackoverflow.com/a/21027742

Version vom 8. Mai 2017, 21:21 Uhr

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 HTML:

<a href="<?php echo base_url();?>controller/method/parameter">...</a>

Or in a JSON-Objects:

{
   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" for clean straight URL's

From now you do not have to bother how to point to ressources anymore. Just start everytime with the name of controller:

In html tags:

<a href="controller/method/parameter">...</a>

Or in a JSON-Objects:

{
   url: "controller/method/parameter"
}

Some Notes

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'],'/'));

Thanks

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".

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)