Creation: 21/07/2009 22:10
Last Modification: 21/07/2009 22:10
When you create a website using a database, you really often use one php page
and generate contents using GET arguments (for example to know which element
of the database to display).
This method could be a problem for search engine who like to have page adresses
with name reflecting the content of the page.
Apache and mod_rewrite can be used to optimize this.
We will take an example where you have a php page displaying all elements
from a category (for example in a blog). Usual way to create links would be
to have links looking like that:
http://mywebsite/myblog/index.php?type=mytype
Something easier to understand for visitors and search engines would be to have:
http://mywebsite/myblog/mytype/index.html
To do this you need to add the following into apache configuration:
RewriteEngine on
RewriteBase /myblog/
RewriteRule ^([a-zA-Z]+)/index.html$ index.php?type=$1
First ligne activates the apache rewrite engine.
RewriteBase is setting the "directory" on which the rule will be applied, this
prevent to write "myblog/" at beginning and in destination of each rule when
you have more then one.
RewriteRule is the actual conversion rule using regular expression. in this we
assume the type is only composed of letters in small and/or big case.
My blog is in fact only one php webpage but using this rule you see directories
corresponding to the categories and page names instead of GET arguments.