Helicon Ape

mod_seo

Overview

mod_seo module provides extended toolset for pattern-based transformation of links on pages to SEO-friendly format.

It may refer to databases or map files for old/new link correspondence, e.g.
old link
http://www.helicontech.com/forum/forum_posts.asp?TID=10731
becomes
http://www.helicontech.com/forum/topic-10731-How_to_make_your_question_more_explicit.html

As well as in mod_linkfreeze all these changes are made on-the-fly and do not require code modification of your application (forum, blog, old site or other)—mod_seo processes each server response, analyses links and replaces only the necessary ones. You'll also be able to change links in your old applications relying on Apache, Tomcat or any other server through proxy module. Unlike in mod_linkfreeze, here you need to write several rules (not one), but the result will be much more readable and SEO-friendly.

Quick start

Here's an example of mod_seo application on our forum :
#put these rules into the /forum/.htaccess file
RewriteEngine On

DBDriver mssql
DBDParams "Data Source=dbServer;Initial Catalog=WebWizForum;User ID=user;Password=password"
DBDPrepareSQL "SELECT Subject     FROM tblTopic  WHERE Topic_ID [email protected]" getSeoTopic
DBDPrepareSQL "SELECT Forum_name  FROM tblForum  WHERE Forum_ID [email protected]" getSeoForum

SetEnv mod_seo

RewriteMap SeoTopic dbd:getSeoTopic
RewriteMap SeoForum dbd:getSeoForum
RewriteMap slug int:slug

SeoRule forum_topics.asp\?FID=(\d+)(?:\&PN=(\d+))?$ \
        $1(?2-$2)-${slug:${SeoForum:$1}}.html [Redirect, Scope=A]
SeoRule forum_posts.asp\?TID=(\d+)(?:\&PN=(\d+)\&TPN=(\d+))?$ \
        topic-$1(?2-$2-$3)-${slug:${SeoTopic:$1}}.html [Redirect,Scope=A]

RewriteRule ^(\d+)-(?:(\d+))?(.*).html forum_topics.asp?FID=$1(?2&PN=$2) [NC,QSA]
RewriteRule ^topic-(\d+)-(?:(\d+)-(\d+)-)?(.*).html forum_posts.asp?TID=$1(?2&PN=$2&TPN=$3) [NC,QSA]

Related articles and topics

Environment variables

Name Context Description
mod_seo S V D .h forces mod_seo to start automatic links replacement on pages
content-type S V D .h allows to explicitly specify the charset value for Content-Type header to be used by mod_seo

Examples

This is how you can enable mod_seo for the entire site

# put this line into the .htaccess under the root of your site
SetEnv mod_seo

and exclude /admin folder from processing by mod_seo

# put this line into the .htaccess under /admin folder
UnsetEnv mod_seo

This same behavior may be achieved with one mod_setenvif directive:

# disables mod_seo for the current context
SetEnvif request_uri !/admin mod_seo

Note! Sometimes you'll need to explicitly specify the charset for mod_seo to use as some applications (like PHP) reset it bypassing IIS leaving Ape modules ignorant. To override Charset parameter value of Content-Type header use the following SetEnv line:

# sets Charset for mod_seo
SetEnv content-type windows-1251

Exact names of charsets may be taken from here .

Directives

Name Context Description
SeoCond S V D .h imposes condition on the subsequent SeoRule directive
SeoRule S V D .h sets the matching pattern and destination path for links replacement

SeoRule

SeoRule directive is used to make links on pages more SEO-friendly. SeoRule is similar to RewriteRule but is applied to links on pages and NOT to requests! Type of links is set by Scope flag.

Syntax

SeoRule Pattern Substitution [flags]

Description

  • Pattern — Perl-compatible regular expression which will be matched against current URL. The current URL may be either originally requested URL or URL already altered by preceding rules. URL never includes protocol, host name and port number.

    Please address Regular expression syntax section of the documentation for more information on building regular expressions.

    Preceding pattern by a ! character will negate entire expression. Negated pattern cannot generate submatches so you cannot use $N references in substitution.

  • Substitution — specifies format string according to which a replacement for the link is generated when the Pattern is matched. In addition to plain text it may include:
    • Special string ' - ' (dash) in Substitution means no substitution and is useful when you need to apply the rule but perform no actions (leave original URL intact)
    • back references to SeoRule pattern: $N ($0-$9) and $$NN ($$10-$$99)
    • RewriteMap expansions using ${mapname:key|default} syntax
    • HTTP header value with %{HTTP:header} syntax
    • server variables %{VARNAME}
    • environment variables %{ENV:variable}
    • conditional format patterns (?Ntrue_string:false_string)
    • grouping parenthesis ‘ ( ‘ and ‘ )

    The syntax also allows escape sequences...

Flags

  • NoCase or NC — makes the Pattern case-insensitive
  • Redirect — performs redirection to SEO-friendly link to avoid duplicate content penalty
  • Scope= None| A |AREA|BASE|FORM|FRAME|HEAD|IFRAME|IMG|INPUT|LINK|SCRIPT|EMBEDDED|ALL — limits the scope of rule application to specific HTML blocks. We recommend to specify only necessary links types for better performance. Default value is A .

Example

#must be placed in the /forum/.htaccess
SetEnv mod_seo

SeoRule forum_topics.asp\?FID=(\d+)$ topic-$1.html [Redirect, Scope=A]

SeoCond

SeoCond directive defines a single condition for the following SeoRule directive.
Multiple conditions for the same rule are possible. When several conditions are applied to the rule, by default, the rule fires when ALL of them get matched (AND logic). If you want OR logic instead (at least one condition must match for the rule to fire), use [OR] flag.

Syntax

SeoCond TestString CondPattern

Description

  • TestString additionally to plain text supports the following constructs:
    • Back references to SeoRule pattern using $N syntax
    • Back references to preceding SeoCond patterns using %N syntax
    • map files calls using ${mapname:key|default} syntax
    • Environment variable using %{ENV:variable} syntax
    • HTTP header value with %{HTTP:header} syntax
    • Server variable using %{NAME_OF_VARIABLE} syntax (Here is the list of supported server variables: )
  • CondPattern specifies a regular expression that will be applied to the instance of TestString . The following special values are also supported :
    • ! — prefix to negate CondPattern
    • <CondPattern — treats CondPattern as a plain string that will be lexicographically compared as "more than"
    • >CondPattern — lexicographically less then comparison
    • =CondPattern — lexicographically equals comparison
    • -d TestString is existing directory
    • -f TestString is existing file
    • -s TestString is a file of non-zero size

    The following values are unsupported because they are senseless in IIS:

    • -l — is a link
    • -x — has executable permissions
    • -F — is existing file, via subrequest
    • -U — is existing URL, via subrequest

Flags

  • NoCase or NC — makes the CondPattern case-insensitive
  • Ornext or OR — combines subsequent SeoCond directives with logical OR instead of implicit AND
  • Optimize or O — normalizes string before processing (removes URL-encoding, illegal characters, etc; also, IIS normalization implies removal of query string from the URL)

Note! SeoCond directive affects only ONE subsequent SeoRule directive.

Advanced example

#replace links that exist in database
RewriteEngine On

DBDriver mssql
DBDParams "Data Source=dbServer;Initial Catalog=WebWizForum;User ID=user;Password=password"
DBDPrepareSQL "SELECT Subject     FROM tblTopic  WHERE Topic_ID [email protected]" getSeoTopic
DBDPrepareSQL "SELECT Forum_name  FROM tblForum  WHERE Forum_ID [email protected]" getSeoForum

SetEnv mod_seo

RewriteMap SeoTopic dbd:getSeoTopic
RewriteMap SeoForum dbd:getSeoForum
RewriteMap slug int:slug

SeoCond ${SeoForum:$1|NOT_FOUND} (.*)
SeoCond %1 !NOT_FOUND 
SeoRule forum_topics.asp\?FID=(\d+)(?:\&PN=(\d+))?$ \
        $1(?2-$2)-${slug:%1}.html [Redirect, Scope=A]

SeoCond ${SeoTopic:$1|NOT_FOUND} (.*)
SeoCond %1 !NOT_FOUND 
SeoRule forum_posts.asp\?TID=(\d+)(?:\&PN=(\d+)\&TPN=(\d+))?$ \
        topic-$1(?2-$2-$3)-${slug:%1}.html [Redirect,Scope=A]

RewriteRule ^(\d+)-(?:(\d+))?(.*).html forum_topics.asp?FID=$1(?2&PN=$2) [NC,QSA]
RewriteRule ^topic-(\d+)-(?:(\d+)-(\d+)-)?(.*).html forum_posts.asp?TID=$1(?2&PN=$2&TPN=$3) [NC,QSA]