将旧产品网址(无ID)重定向到具有基本结构(使用ID)的新网址

时间:2019-05-08 21:27:10

标签: .htaccess mod-rewrite url-rewriting prestashop prestashop-1.7

我的PrestaShop 1.7.5安装遇到问题。

大约2周前,我尝试将PrestaShop版本(从1.7.4升级到1.7.5)。不幸的是,有一个重要的模块不再起作用(调度程序的替代已过时),该模块生成SEO友好的URL,URL中没有ID,例如:domain/{category}/{rewrite}

我们希望不进行任何回滚,并且正在考虑两种选择。

选项1:将所有旧产品网址重定向到新网址

旧网址格式: domain/{category}/{rewrite}

新的网址格式: domain/{parent-category}/{sub-category}/{ID}-{ID_product_attribute}-{rewrite}

选项2:将所有404重定向到searchcontroller

通过使用URL中的关键字来实现此目的,我们可以使用{category}{rewrite}部分

我尝试了很多小代码,就像在网上找到的那样:

RewriteRule ^search/?$ / [R=301,L]
RewriteRule ^search/([^/]+)/?$ /search/$1/1/ [R=301,L]
RewriteRule ^search/([^/]+)/([0-9]+)/?$ search.php?q=$1&category=web&d=$2 [NC,L]

我确实阅读了https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html上的手册 我确实知道如何将一个简单的页面重定向到另一个页面...但是我被卡住了...

这是我现在的.htacces文件(由prestashop生成):


# ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again
# .htaccess automaticaly generated by PrestaShop e-commerce open-source solution
# http://www.prestashop.com - http://www.prestashop.com/forums

<IfModule mod_rewrite.c>
<IfModule mod_env.c>
SetEnv HTTP_MOD_REWRITE On
</IfModule>

RewriteEngine on

#Domain: domain.be
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api$ api/ [L]

RewriteRule ^api/(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]

# Images
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^domain.be$
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]
# AlphaImageLoader for IE and fancybox
RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]

# Dispatcher
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L]
</IfModule>

AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType application/font-woff .woff
AddType font/woff2 .woff2
<IfModule mod_headers.c>
    <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|svg)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/javascript "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-javascript "access plus 1 week"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/x-font-woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    ExpiresByType font/opentype "access plus 1 year"
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/otf "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"
    ExpiresByType application/x-font-otf "access plus 1 year"
</IfModule>

<IfModule mod_headers.c>
    Header unset Etag
</IfModule>
FileETag none
<IfModule mod_deflate.c>
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript application/x-javascript font/ttf application/x-font-ttf font/otf application/x-font-otf font/opentype image/svg+xml
    </IfModule>
</IfModule>

#If rewrite mod isn't enabled
ErrorDocument 404 /index.php?controller=404

# ~~end~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again

谢谢!

2 个答案:

答案 0 :(得分:0)

鉴于产品ID已从您的网址中删除,我认为您无法通过Htaccess重定向它们。 提示:您无法在htaccess文件中逐行添加网址规则。它将减慢您的网站速度。根据您网站的任何要求检查所有规则(css链接,js链接,图像等)

为此,您需要有一个新的控制器来检查和重定向URL。实际上,您需要另一个模块来解决此问题。您的覆盖必须撤消。

-这就是为什么我们从不建议客户从PrestaShop URL中删除ID。

答案 1 :(得分:0)

这有效,并将永久将旧产品网址重定向到新网址(选项1):

  1. 将以下脚本放置在PrestaShop根文件夹中
  2. 可选:编辑id_shopid_lang参数以匹配您的配置
  3. RewriteEngine on之后启动它并将其复制/粘贴到您的.htaccess文件中-删除脚本
  4. Voilà!

脚本:

<?php

include('config/config.inc.php');
include('init.php');

$context = Context::getContext();

$products = Db::getInstance()->ExecuteS('
SELECT p.id_product, pl.link_rewrite prod_url, cl.link_rewrite cat_url
FROM '._DB_PREFIX_.'product p
LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product)
LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default)
WHERE p.active = 1 AND cl.id_lang = 1 AND cl.id_shop = 1 AND pl.id_lang = 1 AND cl.id_shop = 1');

foreach ($products as $p)
{
    $new_url = $context->link->getProductLink(new Product((int)$p['id_product']));
    echo 'RewriteRule ^'.$p['cat_url'].'/'.$p['prod_url'].'$ '.$new_url.' [L,R=301]<br />';
}

输出(在仅包含2种产品的PrestaShop演示商店中):

RewriteRule ^men/hummingbird-printed-t-shirt$ http://localhost/prestashop/1-hummingbird-printed-t-shirt.html [L,R=301]
RewriteRule ^women/brown-bear-printed-sweater$ http://localhost/prestashop/2-brown-bear-printed-sweater.html [L,R=301]
复制/粘贴后

.htaccess文件:

...
<IfModule mod_rewrite.c>
<IfModule mod_env.c>
SetEnv HTTP_MOD_REWRITE On
</IfModule>

RewriteEngine on

# Permanent redirections
RewriteRule ^men/hummingbird-printed-t-shirt$ http://localhost/prestashop/1-hummingbird-printed-t-shirt.html [L,R=301]
RewriteRule ^women/brown-bear-printed-sweater$ http://localhost/prestashop/2-brown-bear-printed-sweater.html [L,R=301]

#Domain: localhost
RewriteRule . - [E=REWRITEBASE:/prestashop/]
RewriteRule ^api$ api/ [L]
...

就转化率而言,选项2对我而言似乎不可行。

我希望这会有所帮助!

如果没有,我会向模块的开发人员索要更新的版本,鉴于这对您来说将是多么耗时,因此25美元的升级成本似乎是合理的。