How to Use Apache rewrite Module for SEO Optimised Addresses

Why I need this

There are many reasons for using mod_rewrite. Lets suppose you have site that loads its contents from database and use http://www.kdelchev.com/index.php file for displaying the information. You may pass id to that file like http://www.kdelchev.com/index.php?p=55 which is not very descriptive and is not user friendly. One more case is that web spiders didn’t rank such URLs.

You will have much better result if you use http://www.kdelchev.com/web-development-tips or http://www.kdelchev.com/web-development-tips.html. One other plus is that way, you hide the used technology for the user, so using mod_rewrite can be more secure.

Do I have mod_rewrite

Here is how to check that you have mod_rewrite installed and running

Answer 1

Go to configuration directory for apache for your distribution. I use Debian and mine is

/etc/apache/mods-enabled

Look there for a link named rewrite. If no link go back one level and enter in mods-aviable. There should be rewrite.load. Make sym link in mods-enabled in mods-enabled folder and restart apache.

Answer 2

If you don’t have access to that directory create simple php file with phpinfo() function and upload it to your server like this:

<?php phpinfo(); ?>

You should see mod_rewrite in the listing. If no contact your hosting company for more details.

How to use mod_rewrite

Just create .htaccess file in the parent directory of your site and put that line in

RewriteEngine on

Now you are ready to make some rules and start using mod_rewrite. Creating rule can be confusing if you didn’t know anything about regular expressions. Don’t worry all comes with the time. Let’s see some examples how to create rules.

Examples

1. Basic Rewriting

This example is straght redirect and is used if you had moved some file at a new location and want all links from the old location to be valid for the new forwarded location.

RewriteEngine onRewriteRule ^old_file_name\.html$ new_file_name.html
  • Here the caret(^) symbols signifies the start of an URL in the current directory. This is the directory where the .htaccess file is placed
  • The dolar($) sign signifies the end of the string to be matched. This is added to stop the matching rule of the URL
  • The dot is special character and is escaped with back slash, which tells Apache to threat it as normal character
2. Folder Redirect

To rewrite the address http://www.kdelchev.com/style_sheets to redirect to file http://www.kdelchev.com/index.php?category=1

RewriteRule ^style_sheets$ index.php?category=1

But what if someone enter http://www.kdelchev.com/style_sheets/ ? The above rule will not work. To correct the problem add one more rule to redirect style_sheets to style_sheets/ like this

RewriteRule ^style_sheets$ style_sheets/RewriteRule ^style_sheets/$ index.php?category=1
3. Regex Redirect

How to redirect http://www.kdelchev.com/style_sheets/how-to-use-styles.html you will have to add one more rule

RewriteRule ^([^/\.]+)/([^/\.]+).html/?$ index.php?category=$1&post=$2

This rule match two regex in the parentheses and redirect index.php with the first match as category and the second with post. You can add as much matches as you want.

4. Visible and Invisible Redirects

All redirect above are invisible. The user type the address and is silently redirected to a new location. Tha address in the browser stays, what the user typed. If you want to show that the user is redirected use [R] at the end of the rule. That way if the user bookmark the address he will take the actual location and no rewriting rule will be used on the next visit.

RewriteEngine onRewriteRule ^old_file_name\.html$ new_file_name.html [R]
5. Making Rewrite Rule Last

You have an option to stop execution of the rewriting rule list if some condition is met. If you have a long list with rules for one file you can stop the execution ot the matching with [L]

RewriteEngine onRewriteRule ^very_old_file\.html$ newer_version.html
RewriteRule ^newer_version\.html$ very_old_file2.html
RewriteRule ^very_old_file2\.html$ file_name.html [L]
RewriteRule ^old_file_name\.html$ new_file_name.html

Here rewriting will skip other rules regarding the very_old_file.html and continue to match other rules

6. Redirect all subdomains and domain.com to www.domain.com
RewriteEngine onRewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
7. Redirect all subdomains except subdomain1.domain.com and subdomain2.domain.com to www.domain.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www|subdomain1|subdomain2\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]

Related Information

RegEx
mod_rewrite doc

Tagged with: , ,
Posted in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sites
Categories
Archives