In a PrestaShop site, the file.htaccess is essential for the proper functioning of numerous features. It is involved in the management of rewritten URLs, security, performance, and maintenance. Understanding it well helps to avoid numerous bugs and fully leverage the capabilities of the platform.
What is the file.htaccess in PrestaShop?
.htaccess is a configuration file for the Apache server. In PrestaShop, this file is automatically generated from the back office, based on the activated options (such as friendly URLs or URL rewriting).
It allows PrestaShop to:
- manage "clean" URLs (e.g.:
/t-shirts/5-tshirt-bleu.htmlinstead ofindex.php?id_product=5) - redirect certain pages (301, 302)
- protect sensitive files or folders
- improve security and performance
| 📝 It is often located at the root of the store, usually in the public folder of your hosting. He may find himself in other places to protect certain files. |
How PrestaShop manages the.htaccess ?
Automatic generation
PrestaShop generates or updates the file..htaccess :
- During theactivation of simplified URLs (SEO & URLs > "Simplified URL")
- During the reset of the URLs via the button "Generate the file"
.htaccessin the back office - During the installation of certain modules
The content is automatically structured between two tags:
# ~~start~~ Do not remove this comment, PrestaShop uses it to build your .htaccess file ... # ~~end~~
| ⚠️ Never modify this block directly by hand: PrestaShop automatically rewrites it. |
Concrete uses of the file.htaccess in PrestaShop
⚠️ The manipulation of the file.htaccessis not recommended for beginners. Even if you have the necessary experience, always back up your .htaccess file before modifying it. |
1. URL rewriting
Allows for readable and SEO-optimized URLs.
RewriteEngine on RewriteRule ^t-shirts/5-tshirt-bleu.html$ product.php?id_product=5 [QSA,L]
Secures certain sensitive files.
Prevents direct access to configuration or log files.
<FilesMatch "\\.(tpl|ini|log|conf|sql)$"> Order Allow,Deny Deny from all </FilesMatch>
3. Forcer HTTPS
Redirects all requests to the HTTPS version of the site.
Also to be activated in the back office: "Enable SSL" in Settings > General.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
4. Custom Redirects
Not recommended for beginners, this option is useful after product deletions, catalog overhauls, URL changes, or redirection to a maintenance page. Different syntaxes exist for different needs.
For example, to redirect to a new product:
Redirect 301 /ancien-produit.html /nouveau-produit.html
Risks associated with improper handling
- Site inaccessible if there is a syntax error
- Navigation issues (redirect loops, errors 500)
- Involuntary deactivation of modules or the front office
- SEO issues (non-indexed pages, errors 404)
Best practices to follow
-
Always make a backup of the file
.htaccessbefore modification. - Do not modify the block automatically generated by PrestaShop (
# ~~start~~ ... # ~~end~~) - Use a reliable text editor (VS Code, Notepad++, etc.)
- Test the modifications in a pre-production environment if possible. There are also online tools to visualize these modifications.
- Please remember to clear the PrestaShop cache after making modifications.
Example of.htaccess typical in PrestaShop.
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirection vers HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# URL rewriting (section générée automatiquement)
# ~~start~~ Do not remove this comment...
RewriteRule ^fr/t-shirts/5-tshirt-bleu.html$ product.php?id_product=5 [QSA,L]
# ~~end~~
# Sécurité
<FilesMatch "\\.(tpl|ini|log|conf|sql)$">
Order Allow,Deny
Deny from all
</FilesMatch>
# Redirection temporaire vers maintenance
# (décommenter si nécessaire)
# RewriteCond %{REQUEST_URI} !^/maintenance.html$
# RewriteCond %{REMOTE_ADDR} !^123\\.456\\.789\\.000$
# RewriteRule ^.*$ /maintenance.html [R=302,L]
</IfModule>
Particular case: Multi-store and.htaccess
In multi-store mode, PrestaShop can create multiple blocks.htaccess for each domain or subfolder. One must be even more vigilant regarding the consistency of the rules if you manually customize the file.