RedirectMatch旧产品网址(没有ID)到新的URL与基本(使用ID)结构(Redirect

2019-09-28 13:13发布

我'面临着与我们的PrestaShop 1.7.5安装问题。

大约2个星期前,我试着提升我们的PrestaShop版本(1.7.4从1.7.5到)。 不幸的是1个重要的模块不能正常工作不再(调度程序的重写是过时的),这个模块生成SEO友好的URL,没有ID的URL中,如下所示: domain/{category}/{rewrite}

我们宁愿做没有回滚和正在考虑两种选择。

选项1:重定向所有老产品的网址到新网址

旧的URL格式: domain/{category}/{rewrite}

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

选项2:重定向所有404 searchcontroller

通过使用来自网址的关键字,来实现这一点,我们可以使用部分{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

提前致谢!

Answer 1:

这工作,将永久重定向老产品的网址以新的(选项#1):

  1. 将下面的脚本在您的PrestaShop根文件夹
  2. 可选:编辑id_shopid_lang参数以匹配您的配置
  3. 启动它,结果复制/粘贴到你的.htaccess文件,刚过RewriteEngine on -删除脚本
  4. 瞧!

脚本:

<?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 />';
}

输出(在的Prestashop示范商店只有2个产品):

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,似乎要升级给予合理的成本如何费时,这将是你。



Answer 2:

鉴于产品ID已经从你的URL删除,我认为你无法通过的.htaccess重定向他们。 提示:您可以不受线的htaccess文件添加URL规则。 它会减慢你的网站。 所有的规则都在从您的网站的任何请求检查(CSS链接,JS链接,图片等)

要做到这一点,你需要有一个新的控制器检查和重定向的URL。 事实上,你需要另一个模块来解决这个问题。 您的覆盖必须扭转。

凹口 - 就是为什么我们从来不建议客户从的Prestashop网址中删除ID。



文章来源: RedirectMatch old product urls (without ID) to new url with basic (using ID) structure