1
357

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:

  1. /cms/site0
    1. index.php
    2. dump.php
    3. robots.txt
    4. sitemap.xml
    5. favicon.ico
    6. .htaccess

Create a file called .htaccess in site0 with the following content:

  1. SetEnv PHP_VER 5
  2. SetEnv REGISTER_GLOBALS 0
  3.  
  4. Options -Indexes
  5. Options +FollowSymLinks
  6.  
  7. ErrorDocument 404 /index.php
  8.  
  9. RewriteEngine On
  10. 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:

  1. <?php
  2. error_reporting(E_ALL | E_STRICT);
  3.  
  4. require_once 'dump.php';
  5.  
  6. dump($_SERVER['REQUEST_URI']);
  7. 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:

  1. function dump($var, $label=null, $echo=true) {
  2.     $label = ($label===null) ? '' : rtrim($label) . '=';
  3.  
  4.     ob_start();
  5.     var_dump($var);
  6.     $output = ob_get_clean();
  7.  
  8.     // remove newlines and tabs
  9.     $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  10.     if (PHP_SAPI == 'cli') {
  11.         $output = PHP_EOL . $label . $output . PHP_EOL;
  12.     }
  13.     else {
  14.         $output = htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
  15.  
  16.         $output = '<pre>' . PHP_EOL . $label . $output . '</pre>'. PHP_EOL;
  17.     }
  18.  
  19.     if ($echo) {
  20.         echo $output;
  21.     }
  22.  
  23.     return $output;
  24. }

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.

  1. User-agent: *
  2. Sitemap: http://www.frasq.org/sitemap.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  3.     <url>
  4.         <loc>http://www.frasq.org/fr/accueil</loc>
  5.         <lastmod>2010-05-13</lastmod>
  6.         <changefreq>daily</changefreq>
  7.         <priority>1.0</priority>
  8.     </url>
  9.     <url>
  10.         <loc>http://www.frasq.org/en/home</loc>
  11.         <lastmod>2010-05-13</lastmod>
  12.         <changefreq>daily</changefreq>
  13.         <priority>1.0</priority>
  14.     </url>
  15. </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:

  1. RewriteEngine On
  2. RewriteRule ^(favicon\.ico|robots\.txt|sitemap\.xml) - [NC,L]
  3. 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

April 3, 2011 at 16:01 by frasq 

Read the article The web developer tools by frasq to install and secure a web server with Apache, MySQL, PHP and phpMyAdmin but also prepare the encryption of the communications with OpenSSL, configure a local email server with Postfix and Dovecot, manage the site online with SSH and Rsync, centralize the source code with Git, limit disk space usage with quotas, set up a local name server with Bind or even watch the activity of the web server with Nagios.

To add a comment, click here.