Pages

Sunday, October 25, 2015

.htaccess file

We have our index.php file set up to process the incoming request and send it to the relevant controller. However, URLs which have the format of index. php?page=some/page/on/our/site or index.php?page=products/view/someproduct are not as attractive or memorable as those with just some/page/on/our/ site or products/view/some-product. With the Apache module mod_rewrite we can get our site to rewrite the more friendly URLs into the less friendly ones for our framework to understand.
ErrorDocument 404 /index.php DirectoryIndex index.php <IfModule mod_rewrite.c>  RewriteEngine on  RewriteCond %{REQUEST_FILENAME} !-f  RewriteCond %{REQUEST_FILENAME} !-d  RewriteRule ^(.*)$ index.php?page=$1 [L,QSA] </IfModule> This .htaccess file instructs the web server (Apache) to use index.php as the index file within a directory. It also instructs Apache that if the mod_rewrite module is enabled, the requests that are not for valid files or directories should be rewritten to the main index.php file. However, anything that occurs after the directory containing the .htaccess file, should be appended to the page $_GET variable. For example, oursite.com/pagea would be rewritten to oursite.com/index. php?page=pagea.

No comments:

Post a Comment