PrestaShop修改SEO规则后旧URL如何重定向至新URL?
Got it, let's tackle this issue head-on. When you update your PrestaShop SEO settings to add .html suffixes to product/category URLs, broken old URLs and missing 301 redirects can tank your SEO and frustrate users. Here are the most reliable solutions to get those redirects working:
1. Enable PrestaShop's Built-in Canonical Redirects
First, check if you're leveraging PrestaShop's native URL handling. Head to your admin panel:
- Go to SEO & URLs > Set up URLs
- Look for the option Auto-redirect to the canonical URL and check the box
- Save your changes
This setting tells PrestaShop to automatically redirect users to the official (canonical) version of a URL. While it might handle some cases, it often needs a boost for suffix changes like adding .html.
2. Bulk Redirects via .htaccess (Best for Large Stores)
For a universal fix that covers all old non-.html URLs, add custom rewrite rules to your root .htaccess file. This is the fastest way to handle hundreds or thousands of products/categories at once.
Open your .htaccess file (located in your PrestaShop root folder) and add these rules right after the RewriteBase / line:
# Redirect non-.html product URLs to .html version RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9]+)-([a-zA-Z0-9-]+)$ $1-$2.html [L,R=301] # Redirect non-.html category URLs to .html version RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-zA-Z0-9-]+)$ $1.html [L,R=301]
Notes:
- Test this immediately by visiting an old non-.html URL (e.g.,
yourstore.com/123-product-nameshould redirect toyourstore.com/123-product-name.html) - If your store uses a custom URL structure (no product ID in URLs), adjust the product rewrite rule to match your setup
- Always back up
.htaccessbefore editing to avoid breaking your site
3. Manual Redirects for Edge Cases
If you have a handful of high-priority pages that aren't covered by the .htaccess rule, use PrestaShop's URL Redirect tool:
- Go to SEO & URLs > URL Redirects
- Click Add new redirect
- Enter the old URL path (e.g.,
/old-category) in the Request URL field - Enter the new .html URL (e.g.,
/old-category.html) in the Target URL field - Set the Type to
Permanent (301)and mark it as active - Save the redirect
4. Database-Level Batch Redirects (Advanced)
If you need granular control or have unique URL structures, you can directly populate PrestaShop's redirect table via SQL. Always back up your database first before running any queries.
Use this SQL query (replace ps_ with your database prefix if it's different) to create redirects for all products:
INSERT INTO ps_redirect (id_redirect, id_shop, id_lang, request_uri, redirect_uri, type, active) SELECT NULL, 1, id_lang, CONCAT('/', link_rewrite), CONCAT('/', link_rewrite, '.html'), '301', 1 FROM ps_product_lang WHERE link_rewrite IS NOT NULL;
For categories, use this query:
INSERT INTO ps_redirect (id_redirect, id_shop, id_lang, request_uri, redirect_uri, type, active) SELECT NULL, 1, id_lang, CONCAT('/', link_rewrite), CONCAT('/', link_rewrite, '.html'), '301', 1 FROM ps_category_lang WHERE link_rewrite IS NOT NULL;
Important:
- Adjust
id_shopto match your store ID (use1for single-store setups) - After running the query, clear PrestaShop's cache to apply the changes
内容的提问来源于stack exchange,提问作者user960971

