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)
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
== Summary ==
== 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.
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.
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.


Example for HTML:
Example for HTML:
Zeile 23: Zeile 23:
</syntaxhighlight>
</syntaxhighlight>


In addition to this the htaccess file redirects everything into the "index.php" and variable "index_page" in "config.php" was cleared.
In addition to this the htaccess file redirects everything into the '''index.php''' and variable '''index_page''' in '''config.php''' was cleared.


From now you do not have to bother how to point to ressources anymore or use the "index.php". Just start everytime with the name of controller:
From now you do not have to bother how to point to ressources anymore or use the '''index.php'''. Just start everytime with the name of controller:


Example for HTML:
Example for HTML:
Zeile 41: Zeile 41:
== 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:


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Zeile 60: Zeile 60:
</syntaxhighlight>
</syntaxhighlight>


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".
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
* 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
* If this htaccess is placed in '''subdir''' the stored value will be: request-dir/request-file


<syntaxhighlight lang="apache">
<syntaxhighlight lang="apache">
Zeile 87: Zeile 87:
</syntaxhighlight>
</syntaxhighlight>


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


<syntaxhighlight lang="apache">
<syntaxhighlight lang="apache">
Zeile 94: Zeile 94:
</syntaxhighlight>
</syntaxhighlight>


If you need to exclude some requests redirecting to "index.php" you can add them in the rewrite condition like here:
If you need to exclude some requests redirecting to '''index.php''' you can add them in the rewrite condition like here:


<syntaxhighlight lang="apache">
<syntaxhighlight lang="apache">
RewriteCond $1 !^(index\.php|assets)
RewriteCond $1 !^(index\.php|assets)
</syntaxhighlight>
</syntaxhighlight>

Version vom 9. Mai 2017, 09:44 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.

Example for HTML:

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

Example for JSON-Objects:

{
   url: "<?php echo base_url();?>controller/method/parameter"
}

CodeCoupler includes a htaccess file that 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 automatically:

<base href="<?=$_SERVER['BASE']?>">

In addition to this the htaccess file redirects everything into the index.php and variable index_page in config.php was cleared.

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

Example for HTML:

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

Example for 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:

Remember:

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)