RedirectMatch url produk lama (tanpa ID) ke url baru dengan struktur dasar (menggunakan ID)

May 08 2019

Saya menghadapi masalah dengan instalasi PrestaShop 1.7.5 kami.

Sekitar 2 minggu yang lalu, saya mencoba meningkatkan versi PrestaShop kami (dari 1.7.4 menjadi 1.7.5). Sayangnya 1 modul penting tidak berfungsi lagi (penggantian dispatcher sudah usang), modul ini menghasilkan URL yang ramah-SEO, tanpa ID di URL, seperti ini:domain/{category}/{rewrite}

Kami lebih memilih untuk tidak melakukan roll-back dan sedang mempertimbangkan dua opsi.

Opsi 1: Alihkan semua URL produk lama ke URL baru

Format url lama: domain/{category}/{rewrite}

Format url baru: domain/{parent-category}/{sub-category}/{ID}-{ID_product_attribute}-{rewrite}

Opsi 2: Alihkan semua 404 ke searchcontroller

Dengan menggunakan kata kunci dari URL, untuk mencapai ini, kita dapat menggunakan bagian {category}&{rewrite}

Saya sudah mencoba banyak kode kecil, yang saya temukan di internet, seperti ini:

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]

Saya membaca manualnya https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html Saya mengerti cara mengalihkan halaman sederhana ke halaman lain ... tetapi saya terjebak ...

Ini adalah file .htacces saya seperti sekarang (dibuat oleh 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

Terima kasih sebelumnya!

Jawaban

BrunoLeveque May 09 2019 at 16:50

Ini berfungsi dan akan secara permanen mengalihkan URL produk lama ke yang baru (Opsi # 1):

  1. Tempatkan skrip di bawah ini di folder root PrestaShop Anda
  2. Opsional: Edit parameter id_shopdan id_langagar sesuai dengan konfigurasi Anda
  3. Luncurkan dan salin / tempel hasilnya ke file .htaccess Anda, tepat setelah RewriteEngine on- hapus skrip
  4. Voilà!

Naskah:

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

Output (di toko demo PrestaShop dengan hanya 2 produk):

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]

File .htaccess setelah salin / tempel:

...
<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]
...

Opsi # 2 tampaknya tidak memungkinkan bagi saya dalam hal rasio konversi.

Saya harap ini membantu!

Jika tidak, saya akan meminta pengembang modul untuk versi yang diperbarui, $ 25 tampaknya merupakan biaya yang masuk akal untuk meningkatkan mengingat betapa memakan waktu ini bagi Anda.

MahdiShad May 09 2019 at 10:25

Mengingat ID produk telah dihapus dari URL Anda, saya rasa Anda tidak dapat mengalihkannya melalui Htaccess. tip: Anda tidak dapat menambahkan aturan url baris demi baris di file htaccess. Ini akan memperlambat situs Anda. Semua aturan diperiksa atas permintaan apa pun dari situs Anda (tautan css, tautan js, gambar, dll)

Untuk melakukan ini, Anda perlu memiliki pengontrol baru yang memeriksa dan mengalihkan URL. Padahal, Anda membutuhkan modul lain untuk memperbaiki masalah ini. Penimpaan Anda harus dibalik.

--Itu sebabnya kami tidak pernah menyarankan pelanggan kami untuk menghapus ID dari URL PrestaShop.