Apache: Convert URL parameter strings to directories for search engine friendly URLS

Imagine you have a URL like http://www.whatever.com/?language=en which looks pretty ugly. For Search Engine Optimization it would be nice to have a more search engine friendly URL like http://www.whatever.com/en.

Apache offers an easy way to hide the parameter string by the second URL format. However, internally the page still is treated like the first version of the URL but users and search engines will not notice the old URL format.

To implement the example from above you simply have to add the following lines to your httpd.conf .

In this example the directory en is forwarded internally to page.php?language=en. If the user enters http://www.whatever.com/en he will see the content of http://www.whatever.com/page.php?language=en without noticing the parameter string.


<Directory "/documentroot/en">

  Options Indexes FollowSymLinks

  AllowOverride All

   Order allow,deny
   Allow from all

  Options +FollowSymlinks
  RewriteEngine on

  RewriteCond %{REQUEST_URI} =/en/

  RewriteRule   ^/? page.php\?language\=en [PT]

</Directory>

In the line /documentroot/en you define the directory for which you want to apply the hack. It has to be the internal location at your server.

The line “RewriteCond %{REQUEST_URI} =/en/” tells Apache to apply the hack only if http://www.whatever.com/en is requested. Other URLs in the directory like http://www.whatever.com/en/anypage.html will not be touched.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: