- User-agent: *
- Sitemap: http://www.frasq.org/sitemap.xml
A single entry point for the whole website
In this chapter, we are going to configure the Apache server so all requests to the site are redirected to just one file: index.php.
To test the result online, enter http://www.frasq.org/cms/site0 in the address bar of your navigator. Try several URLs starting at http://www.frasq.org/cms/site0 like http://local.frasq.org/cms/site0/page.php?q=foo#bar. They all return the same path.
Check that the module rewrite
is activated by Apache.
Under Linux, look for the following line in httpd.conf:
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
NOTE: Depending on the distribution, the configuration of Apache is usually split in several files and you might have to follow the Include
directives until you hit the right one.
Under Windows, the directive which loads the module is a little different:
LoadModule rewrite_module modules/mod_rewrite.so
The recent versions of Apache group all the LoadModule
directives in the directory /etc/apache2/mods_available.
Create a link to the file /etc/apache2/mods_available/rewrite.load in the directory /etc/apache2/mods_enabled:
$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/rewrite.load .
You can also run the command a2enmod
and answer rewrite
to the question.
Create a folder called cms directly at the root of the documents of your web server, in general in the directory /var/www. NOTE: Check the directive DocumentRoot in the configuration of Apache.
Create a folder called site0 in /cms. This first site will have the following content:
- /cms/site0
- index.php
- dump.php
- robots.txt
- sitemap.xml
- favicon.ico
- .htaccess
Create a file called .htaccess in site0 with the following content:
- SetEnv PHP_VER 5
- SetEnv REGISTER_GLOBALS 0
- Options -Indexes
- Options +FollowSymLinks
- ErrorDocument 404 /index.php
- RewriteEngine On
- RewriteRule ^(.*)$ index.php [QSA,L]
NOTE: Don't type the line numbers.
Line 1 tells Apache that the code of the site has been written for PHP version 5.
Line 2 tries to set the variable register_globals
to false
.
NOTE: An internet provider can have a default configuration with PHP 4 and register_globals
to true
.
Line 4 forbids the generation by Apache of the listing of a directory content.
Line 5 allows following symbolic links.
Line 7 redirects addressing errors to index.php.
Line 9 turns on the rewrite engine.
IMPORTANT: If the Apache module rewrite
isn't activated, this directive fails. The working of the site depends on it. Check the error log /var/log/apache2/error.log.
The rule defined line 10 redirects all the paths - ^(.*)$
- to index.php.
Create a file called index.php in site0 with the following content:
- <?php
- error_reporting(E_ALL | E_STRICT);
- require_once 'dump.php';
- dump($_SERVER['REQUEST_URI']);
- dump($_SERVER['SCRIPT_NAME']);
index.php sets the error report level of PHP by calling error_reporting
and loads the code for the dump
function.
The end of the program displays the original HTTP request and the name of the script rewritten by Apache.
NOTE: Not closing a tag <?php
with a tag ?>
at the end of a PHP source code prevents an insertion of a space or a newline character in the output flow.
The function dump
is defined in the file dump.php:
- function dump($var, $label=null, $echo=true) {
- $label = ($label===null) ? '' : rtrim($label) . '=';
- ob_start();
- var_dump($var);
- $output = ob_get_clean();
- // remove newlines and tabs
- $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
- if (PHP_SAPI == 'cli') {
- $output = PHP_EOL . $label . $output . PHP_EOL;
- }
- else {
- $output = htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
- $output = '<pre>' . PHP_EOL . $label . $output . '</pre>'. PHP_EOL;
- }
- if ($echo) {
- echo $output;
- }
- return $output;
- }
dump
returns a string containing the readable representation of $var
in an HTML pre
tag.
If $echo
is true
, which is the case by default, the string is inserted in the output flow.
Assign a text to $label
, typically the name of a variable, to label the output.
Enter http://localhost/cms/site0 in the address bar of your navigator:
string(17) "/cms/site0/"
string(26) "/cms/site0/index.php"
Try http://localhost/cms/site0/search?q=foobar:
string(32) "/cms/site0/search?q=foobar"
string(26) "/cms/site0/index.php"
Placing files called robots.txt and sitemap.xml at the root of a site, gives information to search engines. WIKI: robots.txt and sitemap.xml.
- <?xml version="1.0" encoding="UTF-8"?>
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
- <url>
- <loc>http://www.frasq.org/fr/accueil</loc>
- <lastmod>2010-05-13</lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- <url>
- <loc>http://www.frasq.org/en/home</loc>
- <lastmod>2010-05-13</lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- </urlset>
Enter http://localhost/cms/site0/robots.txt in the address bar of your navigator. The server doesn't return the expected content. Remember that the file .htaccess redirects all requests to index.php, which always returns the same document. To allow a direct access to certain files, add a rule to .htaccess:
- RewriteEngine On
- RewriteRule ^(favicon\.ico|robots\.txt|sitemap\.xml) - [NC,L]
- RewriteRule ^(.*)$ index.php [QSA,L]
Line 10 says that the paths favicon.ico, robot.txt and sitemap.xml are not modified.
Now, if you enter http://localhost/cms/site0/robots.txt, Apache returns the requested document:
User-agent: *
Sitemap: http://www.frasq.org/sitemap.xml
Try http://localhost/cms/site0/sitemap.xml.
Don't forget to add the website icon. WIKI: favicon.ico.
Comments