mod_seo – Helicon Tech Blog http://www.helicontech.com/articles Web Server Enhancements Fri, 22 Jun 2012 14:12:15 +0000 en-US hourly 1 https://wordpress.org/?v=5.2.4 Search Engine Friendly (SEF) URLs for phpBB http://www.helicontech.com/articles/search-engine-friendly-sef-urls-for-phpbb/ Wed, 21 Mar 2012 14:32:53 +0000 http://www.helicontech.com/articles/?p=777 Continue reading ]]> phpBB is the most popular bulletin board engine at the moment. It offers fantastic set of features but unfortunately Search Engine Friendly (SEF or SEO) URLs is not among them. Why you may need to have SEF URLs for your phpBB forum? Because Search Engines give higher rank for pages with keywords in links. Forum is usually a great traffic generator for your web site and by using SEF URLs you can significantly increase its search engine ranking. Additionally these URLs are more pleasant for human eyes since contain readable and understandable keywords and look cleaner.

By default URLs in phpBB are not search or human friendly. So here you can find simple solution how to turn this http://www.example.com/viewtopic.php?f=46&t=212 into this http://www.example.com/Search_Engine_Friendly_URLs_212.html

Our solution is to use Helicon Ape with mod_seo module. Unfortunately Apache does not have mod_seo module, so to use this solution you will need to have phpBB running on Microsoft IIS  web server and install Helicon Ape on it. You can download latest version of Helicon Ape here.

mod_seo is a generic module that allows to modify (grep) portions of HTML output on the fly by using filters and regular expressions. Along with other features provided by Helicon Ape, like rewrite rules, conditions and database key-value maps it turns into a very powerful instrument for SEO. Various forums, blogs, shops and other services with dynamic content can be easily optimized for search engines with this module.

mod_seo can extract all links on the page, compare these links with regular expression patterns, find matching links, extract forum and post numeric IDs from these links, connect to existing forum database, get human readable and keyword reach topic names form database by IDs and then write modified links back to the HTML page before it gets to the user. Then mod_rewrite rules can be used to rewrite URLs back when user requests using these modified links. Main advantage of this technique is that it does not require any change to the forum source code. This means that you do not lose ability to upgrade your forum in future and use standard packages from the code vendor.

So what you need to do to get it working. Suppose you already have phpBB running on Microsoft IIS server. Install Helicon Ape on the server. You can find installation instructions here. By default Helicon Ape will be enabled for all sites on the server, so you may just create .htaccess file in your phpBB folder and put the following content into it:

 


# Enable mod_rewrite and mod_seo for current location
RewriteEngine On
SetEnv mod_seo

# Connect to database
DBDriver mssql
DBDParams "Data Source=(local)\SQLEXPRESS;Initial Catalog=phpbb;User ID=sa;Password=123456"

# Prepare SQL queries to find topic or forum name by ID
DBDPrepareSQL "SELECT forum_name FROM phpbb_forums WHERE forum_id=@KEY" getBBForum
DBDPrepareSQL "SELECT topic_title FROM phpbb_topics WHERE topic_id=@KEY" getBBTopic

# Declare database rewrite maps
RewriteMap BBForum dbd:getBBForum
RewriteMap BBTopic dbd:getBBTopic

# Needed to make strings URL-safe
RewriteMap slug int:slug

# SEO rules to change topic links on pages:

# viewtopic.php?f=46&t=2151&start=15 -> Topic_Title-46-215.html?start=15
SeoRule viewtopic.php\?f=(\d+)\&t=(\d+)(?:&start=(\d+))?$ \
    ${slug:${BBTopic:$2}}-$1-$2.html(?3\?start=$3) [Redirect, Scope=A]

# viewtopic.php?f=6&t=3&p=28#p28 -> Topic_Title-46-215.html?p=23#p=23
SeoRule viewtopic.php\?f=(\d+)\&t=(\d+)\&p=(\d+)$ \
    ${slug:${BBTopic:$2}}-$1-$2.html(?3\?p=$3) [Redirect, Scope=A]

# Check for possible incorrect topic name and redirect to prevent duplicate content
RewriteCond ${slug:${BBTopic:$3}}#$1 ^([^#]+)#(?!\1).+
RewriteRule ^(.+?)-(\d+)-(\d+)\.html$ %1-$2-$3.html [NC,R=301,L]

# Rewrite topic URLs back
RewriteRule ^.+-(\d+)-(\d+)\.html$ viewtopic.php\?f=$1&t=$2 [NC,QSA,L]

# SEO rules to change forum links on pages:

# viewforum.php?f=6&start=100 -> viewforum-Helicon-Zoo-6.html
SeoRule viewforum.php\?f=(\d+)(?:&start=(\d+))?$ \
    ${slug:${BBForum:$1}}-$1.html(?2\?start=$2) [Redirect, Scope=A]

# viewforum.php?f=8&sid=xxxxx -> viewforum-Helicon-Zoo-6.html?sid=xxxx
SeoRule viewforum.php\?f=(\d+)&sid=(\w+)?$ \
    ${slug:${BBForum:$1}}-$1.html?sid=$2 [Redirect, Scope=A]

# Check for possible incorrect forum name and redirect to prevent duplicate content
RewriteCond ${slug:${BBForum:$2}}#$1 ^([^#]+)#(?!\1).+
RewriteRule ^(.+?)-(\d+)\.html$ %1-$2.html [NC,R=301,L]

# Rewrite forum URLs back
RewriteRule ^.+-(\d+)\.html$ viewforum.php\?f=$1 [NC,QSA,L]

 

Don’t forget to use your database user name and password to connect. We are using SQL Server Express as a database for phpBB. For MySQL database connection string may look like this:

 

DBDriver mysql "MySql.Data, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
DBDParams "Data Source=server;Initial Catalog=database; User ID=database;Password=password"

 

The rules above are rather generic and you may want to change them to better meet your specific needs. Nevertheless, after putting them into the .htaccess file SEF URLs on your phpBB forum should immediately start working. New links will appear on forum pages and old links will be redirected to the new format to prevent duplicate content penalty. You can try example of mod_seo optimized phpBB on our community forum.

Hope you will enjoy this solution and wish you highest search rankings!

]]>
Keyword reach URLs in 5 minutes http://www.helicontech.com/articles/mod_seo/ http://www.helicontech.com/articles/mod_seo/#comments Tue, 06 Jul 2010 13:52:00 +0000 http://localhost:85/blog/?p=73 Continue reading ]]> This article is an introduction as well as a detailed guide to new mod_seo module being an integral part of Helicon Ape 3 which is destined to build good-looking URLs based on the application’s DB data or maps without necessity to change anything in its code.

Search Engine Optimization usually means a set of measures to improve web site performance for search engines, and one popular instrument for large electronic sites like forums, blogs, e-shops is Search Engine Friendly (SEF) URLs or Keyword Reach URLs. Such URL usually implies that you will see something like http://www.mysite.com/my_cool_product_name.html in the address bar instead of http://www.mysite.com/products.asp?pid=5568290. Search engines use such keywords in links to better rank pages with these keywords for search requests. Nowadays many web applications support Keyword Reach URLs either internally or by means of mod_rewrite but anyway application level support is required. Many other applications like, for example, version of WebWiz forum we are using on our web site, do not support keywords in links, so we have invented mod_seo to fill in this gap.

The idea is quite easy. Web applications like forums or blogs usually use database to store topics, posts and articles, identify posts by some IDs and use these IDs in links. This database already exists and we can easily connect to it and bind these IDs to the topic titles, where all these valuable keywords are stored.

mod_seo introduces only one directive – SeoRule – with the syntax very close to that of RewriteRule directive in mod_rewrite, but SeoRule rewrites links on pages instead of rewriting requested URLs. Together with RewriteRule and RewriteMap directives and database support (already built into Helicon Ape engine) we can make a configuration to retract keywords from database, put these keywords in the links on pages and then navigate user around the site without changing a single line of code in the web application (forum in our case).

Now we will show you how to turn this http://www.helicontech.com/forum/forum_posts.asp?TID=15116 into this http://www.helicontech.com/forum/15116-RewriteMap_and_RewriteRule_and_querystring.html step by step using our live forum as example.

What we do:

  1. Make sure mod_rewrite, mod_dbd and mod_seo modules are enabled in httpd.conf
    Enabling Helicon Ape modules
  2. Put the following lines into .htaccess in the root of our site:
    DBDriver mssql
    # include DBDParams
    DBDParams "Data Source=(local)\SQLEXPRESS;Initial Catalog=WebWizForum;User ID=sa;Password=*****"
    DBDPrepareSQL "SELECT [Subject]     FROM [WebWizForum].[dbo].[tblTopic]  WHERE [Topic_ID] =@KEY" \
                  getSeoTopic
    DBDPrepareSQL "SELECT [Forum_name]  FROM [WebWizForum].[dbo].[tblForum]  WHERE [Forum_ID] =@KEY" \
                  getSeoForum
    
    RewriteMap SeoTopic dbd:getSeoTopic
    RewriteMap SeoForum dbd:getSeoForum
    RewriteMap slug int:slug
    
    #SEO-ing
    SeoRule forum_topics\.asp\?FID=(\d+)\&title=([^\s"'$&]*) \
            forum$1-$2.html [Redirect,Scope=A]
    SeoRule forum_topics.asp\?FID=(\d+)&PN=(\d+)$ \
            forum$1-${slug:${SeoForum:$1|not-found}}-$2.html [Redirect,Scope=A]
    SeoRule forum_topics.asp\?FID=(\d+)$ \
            forum$1-${slug:${SeoForum:$1|not-found}}.html [Redirect,Scope=A]
    
    SeoRule forum_posts.asp\?TID=(\d+)\&title=([^\s"'$&]*)(?:&(get=last#\d+))? \
            $1-$2.html?4\?$4 [Redirect,Scope=A]
    SeoRule forum_posts.asp\?TID=(\d+)(?:\&PN=(\d+)\&TPN=(\d+))?(?:&(get=last#\d+))?$ \
            $1-${slug:${SeoTopic:$1|not-found}}(?2-$3).html?4\?$4 [Redirect,Scope=A]
    
    #rewrite to the original ones
    RewriteRule forum(\d+)-.*(?:-(\d+))\.html$ /forum/forum_topics.asp\?FID=$1&PN=$2 [NC,QSA,L]
    RewriteRule forum(\d+)-.*\.html$ /forum/forum_topics.asp\?FID=$1 [NC,QSA,L]
    
    RewriteRule (\d+)-.*(?:-(\d+))\.html$ /forum/forum_posts.asp\?TID=$1&TPN=$2 [NC,QSA]
    RewriteRule (\d+)-.*\.html$ /forum/forum_posts.asp\?TID=$1 [NC,QSA]

Now with explanations:

First we need to declare database maps (RewriteMap with database connection) to use in link replacing rule.

  • Declare Microsoft SQL Server driver:
    DBDriver mssql
  • And include DBDParams string for correct SQL connection
    DBDParams "Data Source=(local)\SQLEXPRESS;Initial Catalog=WebWizForum;User ID=sa;Password=*****"
  • Prepare SQL queries to select proper fields (topic/forum name) from corresponding tables of the forum database. I was using Microsoft SQL Server Management Studio to do this. Though any database management tool will do. A ‘@KEY’ in the query has a special meaning and will be substituted by the key value when map call is executed. Declaration is needed to assign names for the queries to refer to them in RewriteMap declaration. We have choosen getSeoTopic and getSeoForum as names for them.
    DBDPrepareSQL "SELECT [Subject]     FROM [WebWizForum].[dbo].[tblTopic]  WHERE [Topic_ID] =@KEY" \
                  getSeoTopic
    DBDPrepareSQL "SELECT [Forum_name]  FROM [WebWizForum].[dbo].[tblForum]  WHERE [Forum_ID] =@KEY" \
                  getSeoForum

    Topics table and corresponding SELECT query
    Topics table and corresponding SELECT query

    Forums table and corresponding SELECT query
    Forums table and corresponding SELECT query

  • Now declare database-driven maps using previously declared queries. These maps will be referred to in subsequent SeoRule directives by their names – SeoTopic and SeoForum.
    RewriteMap SeoTopic dbd:getSeoTopic
    RewriteMap SeoForum dbd:getSeoForum
  • Define slug map that is used to remove special characters from topic names to make our links more friendly for browsers and user eyes.
    RewriteMap slug int:slug
  • Now it is time for SeoRule directives.
    New URL format for main forum pages includes the forum number FID and the forum name taken from the database: http://www.helicontech.com/forum/forum11-Helicon_Ape.html
    [Redirect] flag forces requests to old-style links to be redirected to new analogues (for the above link it’s http://www.helicontech.com/forum/forum_topics.asp?FID=11)
    [Scope=A] says mod_seo to search and replace URLs in tags only
    SeoRule forum_topics.asp\?FID=(\d+)&PN=(\d+)$ \
            forum$1-${slug:${SeoForum:$1|not-found}}-$2.html   [Redirect,Scope=A]
    SeoRule forum_topics.asp\?FID=(\d+)$ \
            forum$1-${slug:${SeoForum:$1|not-found}}.html   [Redirect,Scope=A]
  • New URL format for topic pages includes the topic number TID (as the topic name is not unique) and the topic name taken from the db: http://www.helicontech.com/forum/10000-ISAPI_and_ZEND_Framework.html
    [Redirect] flag will deal with its ancestor: http://www.helicontech.com/forum/forum_posts.asp?TID=10000)
    SeoRule forum_posts.asp\?TID=(\d+)(?:\&PN=(\d+)\&TPN=(\d+))?(?:&(get=last#\d+))?$ \
            $1-${slug:${SeoTopic:$1|not-found}}(?2-$3).html?4\?$4   [Redirect,Scope=A]
  • As the forum engine doesn’t understand the new URL format, we use rewriting to convert it to habitual one, e.g. http://www.helicontech.com/forum/forum_posts.asp?TID=10000 for forum topics
    RewriteRule ^forum/forum(\d+)-.*(?:-(\d+))\.html$ /forum/forum_topics.asp\?FID=$1&PN=$2 [NC,QSA,L]
    RewriteRule ^forum/forum(\d+)-.*\.html$ /forum/forum_topics.asp\?FID=$1 [NC,QSA,L]

    and for forum posts

    RewriteRule ^forum/(\d+)-.*(?:-(\d+))\.html$ /forum/forum_posts.asp\?TID=$1&TPN=$2 [NC,QSA]
    RewriteRule ^forum/(\d+)-.*\.html$ /forum/forum_posts.asp\?TID=$1 [NC,QSA]
  • The result is a page like this:
    Forum with mod_seo enabled

The pleasant thing is you don’t need to change anything in your application — mod_seo and mod_rewrite take care of all cycle of transformations and you just enjoy the SEF picture.

Same technique can be used for many other database driven applications like forums, blogs or e-shops. Existing database may be used to transform links or new maps can be created.

If you need assistance with doing same thing for your application, you can get a Premium Support Plan from us and our support team will be happy to tailor a custom solution for you!

Best regards,
Yaroslav, Anton — HeliconTech Team

]]>
http://www.helicontech.com/articles/mod_seo/feed/ 9