Category duplicate content solution

This redirects (301 moved permanently) url requests missing trailing slash / on category to category with trailing slash, only if seo category type selected is non file (trailing slash /). Example request for domain.com/tools redirects (301) to domain.com/tools/



Make a backup of func.php in /addons/seo before proceeding.



In function fn_seo_get_route (about line 278) find the following code:


// For the locations wich names stored in the table
if (!empty($objects) && !empty($objects['object_name'])) {
$_seo = db_get_row("SELECT * FROM ?:seo_names WHERE name = ?s", $objects['object_name']);

if (empty($_seo) && !empty($objects['extension'])) {
$_seo = db_get_row("SELECT * FROM ?:seo_names WHERE name = ?s", $objects['object_name'] . '.' . $objects['extension']);
}




Then add this code:



if ($_seo['type'] == 'c')
{
$slash = strlen($_SERVER['REQUEST_URI']) - strlen(rtrim($_SERVER['REQUEST_URI'], '/'));
if (Registry::get('addons.seo.seo_category_type') != 'file' && $slash != 1)
{
if (!empty($_SERVER['REDIRECT_URL']) && (strpos($_SERVER['REQUEST_URI'], '?') === false) {
$redirect_url = (defined('HTTPS') ? 'https://' . $config['https_host'] : 'http://' . $config['http_host']) . rtrim($_SERVER['REDIRECT_URL'], '/') . '/';
header("HTTP/1.1 301 Moved Permanently");
fn_redirect($redirect_url, true);
}
}
}




This will ensure that categories do not end up with duplicate content, and all non trailing slash requests are redirected to trailing slash equivalent.



Below is alternative code for extensionless category file types (example: domain.com/tools domain.com/tools/screw-drivers)


if ($_seo['type'] == 'c')
{
$slash = strlen($_SERVER['REQUEST_URI']) - strlen(rtrim($_SERVER['REQUEST_URI'], '/'));
if (Registry::get('addons.seo.seo_category_type') == 'file' && $slash != 0)
{
if (!empty($_SERVER['REDIRECT_URL'])) {
$redirect_url = (defined('HTTPS') ? 'https://' . $config['https_host'] : 'http://' . $config['http_host']) . rtrim($_SERVER['REDIRECT_URL'], '/');
header("HTTP/1.1 301 Moved Permanently");
fn_redirect($redirect_url, true);
}
}
}

Updated to prevent query string being redirected to url with trailing /. So sort by and view options now work on categories

this is wrong / error in code…



if ($_seo['type'] == 'c')
{
$slash = strlen($_SERVER['REQUEST_URI']) - strlen(rtrim($_SERVER['REQUEST_URI'], '/'));
if (Registry::get('addons.seo.seo_category_type') != 'file' && $slash != 1)
{
if (!empty($_SERVER['REDIRECT_URL']) && (strpos($_SERVER['REQUEST_URI'], '?') === false) {
$redirect_url = (defined('HTTPS') ? 'https://' . $config['https_host'] : 'http://' . $config['http_host']) . rtrim($_SERVER['REDIRECT_URL'], '/') . '/';
header("HTTP/1.1 301 Moved Permanently");
fn_redirect($redirect_url, true);
}
}
}





should be



if ($_seo['type'] == 'c')
{
$slash = strlen($_SERVER['REQUEST_URI']) - strlen(rtrim($_SERVER['REQUEST_URI'], '/'));
if (Registry::get('addons.seo.seo_category_type') != 'file' && $slash != 1)
{
if (!empty($_SERVER['REDIRECT_URL']) && (strpos($_SERVER['REQUEST_URI'], '?') === false) ) {
$redirect_url = (defined('HTTPS') ? 'https://' . $config['https_host'] : 'http://' . $config['http_host']) . rtrim($_SERVER['REDIRECT_URL'], '/') . '/';
header("HTTP/1.1 301 Moved Permanently");
fn_redirect($redirect_url, true);
}
}
}




missing ) at

$_SERVER['REQUEST_URI'], '?') === false) )



furthermore, this code only works id i place AFTER THE } closing tag of



if (empty($_seo) && !empty($objects['extension'])) {
$_seo = db_get_row("SELECT * FROM ?:seo_names WHERE name = ?s ?p", $objects$
if (empty($_seo)) {
$_seo = db_get_row("SELECT * FROM ?:seo_names WHERE name = ?s", $ob$
}


}




and not BEFORE like you say

I got a simple solution, no queries and no speed increase, only .htaccess modified



RewriteCond %{REQUEST_URI} !.(php|html?|jpg|gif|png|css|js|tpl|flv|swf|xml|txt|pdf|ico)$

RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]


  • is working for the root stores

    if you have RewriteBase /store, please use



    RewriteCond %{REQUEST_URI} !.(php|html?|jpg|gif|png|css|js|tpl|flv|swf|xml|txt|pdf)$

    RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/store/$1$2/ [L,R=301]

[size=3][font=verdana, geneva, sans-serif]So direct links to /product redirects to /product/ and /category redirects to /category/ ?



What about /checkout or /cart and other paths without specific file names in the path? Would you have to use the “SEO Rules Manager” within the Admin panel?





@ VALI

Thanks for your suggestion.



This change doesn't seem to do anything for me. I do not see a trailing slash “/” for any category, when i visit a category URL without the “/”, whether or not if I use these changes. Strange?



Would something along the lines of this also work?


RewriteRule ^(.*)([^/])$ ./path/to/shop/$1$2/ [R=301,L]