Mods: Automatic Base Detection: Unterschied zwischen den Versionen
Tm (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Tm (Diskussion | Beiträge) |
||
| Zeile 3: | Zeile 3: | ||
Sometimes it is difficult to move an application from one subfolder into another or into the root folder. Another problem is moving your applcation to another domain. It would be much simpler if the application use only relative url's and all the other stuff would be done automatically. | Sometimes it is difficult to move an application from one subfolder into another or into the root folder. Another problem is moving your applcation to another domain. It would be much simpler if the application use only relative url's and all the other stuff would be done automatically. | ||
In the most cases there are at least two points in your application you must always have to remember to modify if you move your application around. If you are using redirections with '''mod_rewrite''' you have to modify at least the '''RewriteBase''' and the most frameworks need extra configuration and special code to generate relative | In the most cases there are at least two points in your application you must always have to remember to modify if you move your application around. If you are using redirections with '''mod_rewrite''' you have to modify at least the '''RewriteBase''' and the most frameworks need extra configuration and special code to generate a relative URL. | ||
With CodeIgniter for example you have functions like '''base_url()''' or '''site_url()''' which should helps you to built links without to know about your current place. But this not very handy as you have always remember to use these functions in every link and to setup correctly your configuration value in '''$config['base_url']'''. An automatic base url detection is built in, but for security reasons the IP address of the your server will be used to compose the URI instead of your host name ([https://expressionengine.com/blog/http-host-and-server-name-security-issues read more about security concerns of detecting the host name automatically]). This will not work in the most cases of course. Furthermore these functions produce absolute URLs, which is not necessary imo. | With CodeIgniter for example you have functions like '''base_url()''' or '''site_url()''' which should helps you to built links without to know about your current place. But this not very handy as you have always remember to use these functions in every link and to setup correctly your configuration value in '''$config['base_url']'''. An automatic base url detection is built in, but for security reasons the IP address of the your server will be used to compose the URI instead of your host name ([https://expressionengine.com/blog/http-host-and-server-name-security-issues read more about security concerns of detecting the host name automatically]). This will not work in the most cases of course. Furthermore these functions produce absolute URLs, which is not necessary imo. | ||
Version vom 22. November 2017, 14:47 Uhr
Summary
Sometimes it is difficult to move an application from one subfolder into another or into the root folder. Another problem is moving your applcation to another domain. It would be much simpler if the application use only relative url's and all the other stuff would be done automatically.
In the most cases there are at least two points in your application you must always have to remember to modify if you move your application around. If you are using redirections with mod_rewrite you have to modify at least the RewriteBase and the most frameworks need extra configuration and special code to generate a relative URL.
With CodeIgniter for example you have functions like base_url() or site_url() which should helps you to built links without to know about your current place. But this not very handy as you have always remember to use these functions in every link and to setup correctly your configuration value in $config['base_url']. An automatic base url detection is built in, but for security reasons the IP address of the your server will be used to compose the URI instead of your host name (read more about security concerns of detecting the host name automatically). This will not work in the most cases of course. Furthermore these functions produce absolute URLs, which is not necessary imo.
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"
}
So why not using only relative URLs?
CodeCoupler includes a htaccess file that detects and save the current base folder in an environment variable. This variable will be used to set an absolute URL into the configuration variable base_url in codeigniter. From now on all CodeIgniter functions will produce an URI without a hostname and with the correct subfolder if needed.
In an HTML page you can set your base like this:
<base href="<?= base_url() ?>">
By the way: If you are using the template html it will be already set in every page automatically.
From now you do not have to bother how to point to ressources anymore. 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
With this modification all the libraries using base_url(), site_url() or some helpers like redirect() which call these function will work in the most cases like before. The only limitaion is the usage of the second paramter $protocoll of the both functions. As we use only absolute paths and not any host name or IP address, we cannot set a protocol. The functions will show an error if you try to use the htaccess file and call them with a protocol.
If you nevertheless should really need this functionality you must set your domain name in the variable base_url in config.php. You could of course setup the following code to use your domain name and benefiting from the automatic base detection, The variable {CC_BASE_URL} will be automatically replaced with the detected base:
$config['base_url'] = 'http://YOUR_FIXED_DOMAIN/{CC_BASE_URL}'));
How this works
Every line of the htaccess file explained:
RewriteCond %{ENV:CC_TMP_URI} ^$
RewriteRule ^(.*)$ - [ENV=CC_TMP_URI:$1]
The first line looks if the variable CC_TMP_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 CC_TMP_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:CC_BASE_URL} ^$
RewriteCond %{ENV:CC_TMP_URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=CC_BASE_URL:%2]
The first line looks if the variable CC_BASE_URL 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:
- Our example Request: http://host/subdir/request-dir/request-file?var=val
- The variable CC_TMP_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 CC_BASE_URL. And we are ready for our final rewrite:
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ %{ENV:CC_BASE_URL}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)