Author: Trotter
Email: [email protected]
Source: www.gbunix.com
Please keep the document complete and indicate the source when reprinting.
Preface
As the content on the Internet grows at an alarming rate, the importance of search engines has become more and more prominent. If a website wants to be better indexed by search engines, in addition to being user-friendly (User Friendly), the website design must also be search engine friendly. The design of (Search Engine Friendly) is also very important. The more page content that enters the search engine, the greater the chance of being found by users using different keywords. I have to admit that rewriting dynamic web links into static links is the safest and most stable way to optimize search engines. This solution is proposed for the URL redirection of the phpBB forum system.
Solution
URL redirection can technically be implemented in two ways, one is based on URL rewrite, and the other is based on PATH_INFO. For example, http://www.gbunix.com/bbs/ftopic102.html is implemented based on rewrite, and http://www.gbunix.com/article/article.php/515 is implemented based on PATH_INFO.
For the transformation of the PHPBB forum, we will introduce these two technologies respectively.
1. Implementation using rewrite technology:
Modify the phpBB code:
Open the /includes/page_header.php file and
search for the code:
//
//Generate logged in/logged out status
//
Add before:
ob_start();
function replace_for_mod_rewrite(&$s)
{
$urlin =
array(
"'(?<!/)viewforum.php?f=([0-9]*)&topicdays=([0-9]*)&start=([0-9]*)'",
"'(?<!/)viewforum.php?f=([0-9]*)&mark=topics'",
"'(?<!/)viewforum.php?f=([0-9]*)'",
"'(?<!/)viewtopic.php?t=([0-9]*)&view=previous'",
"'(?<!/)viewtopic.php?t=([0-9]*)&view=next'",
"'(?<!/)viewtopic.php?t=([0-9]*)&postdays=([0-9]*)&postorder=([a-zA-Z]*)& ;start=([0-9]*)'",
"'(?<!/)viewtopic.php?t=([0-9]*)&start=([0-9]*)&postdays=([0-9]*)&postorder =([a-zA-Z]*)&highlight=([a-zA-Z0-9]*)'",
"'(?<!/)viewtopic.php?t=([0-9]*)start=([0-9]*)'",
"'(?<!/)viewtopic.php?t=([0-9]*)'",
"'(?<!/)viewtopic.php&p=([0-9]*)'",
"'(?<!/)viewtopic.php?p=([0-9]*)'",
);
$urlout = array(
"viewforum\1-\2-\3.html",
"forum\1.html",
"forum\1.html",
"ptopic\1.html",
"ntopic\1.html",
"ftopic\1-\2-\3-\4.html",
"ftopic\1.html",
"ftopic\1-\2.html",
"ftopic\1.html",
"sutra\1.html",
"sutra\1.html",
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}
Open the /includes/page_tail.php file and
search for the code:
$db->sql_close();
and then add:
$contents = ob_get_contents();
ob_end_clean();
echo replace_for_mod_rewrite($contents);
global $dbg_starttime;
If your phpBB is version 2.06, open the includes/functions.php file and
search for the code:
if (!empty($db))
{
$db->sql_close();
}
:
if (stristr($url, 'http://')) {
header('Location: ' . $url);
exit;
}
Finally create the .htaccess file in the bbs directory. The file content is:
RewriteEngine On
RewriteRule ^forums.* index.php
RewriteRule ^forum([0-9]*).* viewforum.php?f=$1&mark=topic
RewriteRule ^viewforum([0-9]*)-([0-9]*)-([0-9]*).* viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^forum([0-9]*).* viewforum.php?f=$1
RewriteRule ^ptopic([0-9]*).* viewtopic.php?t=$1&view=previous
RewriteRule ^ntopic([0-9]*).* viewtopic.php?t=$1&view=next
RewriteRule ^ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* viewtopic.php?t=$1&postdays =$2&postorder=$3&start=$4
RewriteRule ^ftopic([0-9]*)-([0-9]*).* viewtopic.php?t=$1&start=$2
RewriteRule ^ftopic([0-9]*).* viewtopic.php?t=$1
RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5
RewriteRule ^sutra([0-9]*).* viewtopic.php?p=$1
If your server does not support .htaccess, please open the httpd.conf file and edit your virtual host part as follows:
<VirtualHost 1.2.3.4 >
ServerAdmin [email protected]
DocumentRoot /home1/ftp/trotter/www
ServerName www.gbunix.com
RewriteEngineOn
RewriteRule ^/bbs/forums.* /bbs/index.php
RewriteRule ^/bbs/forum([0-9]*).* /bbs/viewforum.php?f=$1&mark=topic
RewriteRule ^/bbs/viewforum([0-9]*)-([0-9]*)-([0-9]*).* /bbs/viewforum.php?f=$1&topicdays=$2&start=$3
RewriteRule ^/bbs/forum([0-9]*).* /bbs/viewforum.php?f=$1
RewriteRule ^/bbs/ptopic([0-9]*).* /bbs/viewtopic.php?t=$1&view=previous
RewriteRule ^/bbs/ntopic([0-9]*).* /bbs/viewtopic.php?t=$1&view=next
RewriteRule ^/bbs/ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).* /bbs/viewtopic. php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^/bbs/ftopic([0-9]*)-([0-9]*).* /bbs/viewtopic.php?t=$1&start=$2
RewriteRule ^/bbs/ftopic([0-9]*).* /bbs/viewtopic.php?t=$1
RewriteRule ^/bbs/ftopic([0-9]*).html /bbs/viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5
RewriteRule ^/bbs/sutra([0-9]*).* /bbs/viewtopic.php?p=$1
ErrorLog logs/gbunix.com-error_log
CustomLog logs/gbunix.com-access_log combined
</VirtualHost>
If you are not using a virtual host, just put the RewriteRule part of the code at the end of the httpd.conf file.
Note: It is very important that for the security of the system, please create the robots.txt file in the bbs release directory. The file content is as follows:
Disallow: /your-forum-folder/sutra*.html$
Disallow: /your-forum-folder/ptopic*.html$
Disallow: /your-forum-folder/ntopic*.html$
Disallow: /your-forum-folder/ftopic*asc*.html$
Install the mod_rewrite module for apache.
If your server apache has not been installed, it is very simple. Just compile the mod_rewrite module into it when compiling apache. Related documents can be found at Found at www.gbunix.com . If your apache has been installed, and now you just want to compile the mod_rewrite.so module and load it in apache, we will introduce this method below.
Take the Solaris operating system as an example:
# PATH=/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin:$PATH
# export PATH
# which gcc
# which make
# find ./ -name mod_rewrite.c //Look for the mod_rewrite.c file in the apache installation directory
# cd PATH/to/mod_rewrite.c //Enter the directory containing the mod_rewrite.c file
# apxs -c mod_foo.c //Please specify the absolute path for apxs, in the bin directory where you are currently using apache
# apxs -i -a -n mod_rewrite mod_rewrite.la
If there are no errors, a mod_rewrite.so file should be compiled in the modules directory of your apache.
Edit the httpd.conf file and confirm that httpd.conf already contains the loading statement of mod_rewrite.so, as follows:
LoadModule rewrite_module modules/mod_rewrite.so
At this time, your apache should already support rewrite.
2. Implementation based on PATH_INFO technology:
Modify the phpBB code:
Open the overall_header.tpl file and add the following code to the first line:
<base href=" http://www.your-forum.com/forum-dir/ ">
Open config. php file, add the following code before ?>:
if ($REQUEST_METHOD == "GET") {
if (strlen(getenv('PATH_INFO')) > 1) {
$GET_array = array();
$PHP_SELF = str_replace(getenv('PATH_INFO'), '', $PHP_SELF);
$vars = explode('/', substr(getenv('PATH_INFO'), 1));
for ($i=0, $n=sizeof($vars); $i<$n; $i++) {
if (strpos($vars[$i], '[]')) {
$GET_array[substr($vars[$i], 0, -2)][] = $vars[$i+1];
} else {
$HTTP_GET_VARS[$vars[$i]] = $vars[$i+1];
}
$i++;
}
if (sizeof($GET_array) > 0) {
while (list($key, $value) = each($GET_array)) {
$HTTP_GET_VARS[$key] = $value;
}
}
}
}
if ($REQUEST_METHOD == "POST") {
if (strlen(getenv('PATH_INFO')) > 1) {
$POST_array = array();
$PHP_SELF = str_replace(getenv('PATH_INFO'), '', $PHP_SELF);
$vars = explode('/', substr(getenv('PATH_INFO'), 1));
for ($i=0, $n=sizeof($vars); $i<$n; $i++) {
if (strpos($vars[$i], '[]')) {
$POST_array[substr($vars[$i], 0, -2)][] = $vars[$i+1];
} else {
$HTTP_POST_VARS[$vars[$i]] = $vars[$i+1];
}
$i++;
}
if (sizeof($GET_array) > 0) {
while (list($key, $value) = each($POST_array)) {
$HTTP_POST_VARS[$key] = $value;
}
}
}
}
Open the functions.php file and add the following code before ?>:
function replace_for_mod_rewrite($s) {
$s = str_replace("?", "/", $s);
$s = str_replace("&", "/", $s);
$s = str_replace("&", "/", $s);
$s = str_replace("=", "/", $s);
return $s;
}
Open the sessions.php file and replace the originally defined append_sid() function with the following code:
function append_sid($url, $non_html_amp = false)
{
global $SID;
if ( !empty($SID) && !preg_match('#sid=#', $url) && !preg_match('#sid/#', $url) && !stristr( $_SERVER["HTTP_USER_AGENT"] ,'bot ') && !stristr($_SERVER["HTTP_USER_AGENT"] ,'inktomi'))
{
$url .= ( ( strpos($url, '?') != false ) ? ( ( $non_html_amp ) ? '&' : '&' ) : '?' ) . $SID ;
}
$url=replace_for_mod_rewrite($url);
return $url;
}
At this time, your forum URL will be mapped to ( http://www.domain/bbs/viewtopic.php/t/4 ).
References: