使用htaccess防止在类别中解析不存在的路径

时间:2014-02-24 14:27:59

标签: .htaccess opencart

OpenCart商店目前允许URL到不存在的位置解析并返回状态200 OK。

例如: -

http://www.domain.com/real-category

这允许此路径之后的任何内容加载/实际分类并返回200 OK并在Google中编入索引,例如: -

http://www.domain.com/real-category/456464564
http://www.domain.com/real-category/sdgsgsdsd
http://www.domain.com/real-category/type-anything-here-and-the-url-works

我在.htaccess中有以下内容(在我认为与此查询无关的许多其他规则中)并希望知道是否有一些我可以添加的内容来处理上述情况为不存在的路径返回404 not found错误?

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

1 个答案:

答案 0 :(得分:0)

首先,让我们了解OC如何处理友情网址:

在htaccess中,此规则通常是入口点: RewriteRule ^([^?] *)index.php? route = $ 1 [L,QSA]

index.php实例在preAction数组上有一些Action类,其中一个是Action('common/seo_url')。在他的索引方法上我们有这个代码:

$parts = explode('/', $this->request->get['_route_']);

foreach ($parts as $part) {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");

    if ($query->num_rows) {
        $url = explode('=', $query->row['query']);

        if ($url[0] == 'product_id') {
            $this->request->get['product_id'] = $url[1];
        }

        if ($url[0] == 'category_id') {
            if (!isset($this->request->get['path'])) {
                $this->request->get['path'] = $url[1];
            } else {
                $this->request->get['path'] .= '_' . $url[1];
            }
        }

        if ($url[0] == 'manufacturer_id') {
            $this->request->get['manufacturer_id'] = $url[1];
        }

        if ($url[0] == 'information_id') {
            $this->request->get['information_id'] = $url[1];
        }
    } else {
        $this->request->get['route'] = 'error/not_found';
    }
}

if (isset($this->request->get['product_id'])) {
    $this->request->get['route'] = 'product/product';
} elseif (isset($this->request->get['path'])) {
    $this->request->get['route'] = 'product/category';
} elseif (isset($this->request->get['manufacturer_id'])) {
    $this->request->get['route'] = 'product/manufacturer/info';
} elseif (isset($this->request->get['information_id'])) {
    $this->request->get['route'] = 'information/information';
}

基本上,这将根据此首选项顺序获得最后一个有效段:产品,类别,制造商,信息页面。要解决您的问题,您需要检查网址上是否有任何无效的细分。我能想象的最简单的方法是检查'route'数组上的$this->request->get索引的值,如果它的某个段的相等'error/not_found'无效,则取消设置valids段结果:

...
            $this->request->get['information_id'] = $url[1];
        }
    } else {
        $this->request->get['route'] = 'error/not_found';
    }
}

if(isset($this->request->get['route']) && $this->request->get['route'] == 'error/not_found'){
    unset($this->request->get['product_id'], $this->request->get['path'], $this->request->get['manufacturer_id'], $this->request->get['information_id']);
}

if (isset($this->request->get['product_id'])) {
    $this->request->get['route'] = 'product/product';
} elseif (isset($this->request->get['path'])) {
    $this->request->get['route'] = 'product/category';
...

非常简单,但{h}使用rewrite rules。我认为这是不可能的......

[编辑]

请不要在OC上编辑核心文件,而是在检查$this->request->get['product_id'])之前使用vQmod添加这些行。