Knowledge base

1000 FAQs, 500 tutorials and instructional videos. Here, there are only solutions!

Using URL rewriting

This guide explains the principle of URL rewriting (URL Rewriting), an essential technique for SEO and user experience.

 

Preamble

  • Aesthetics and Clarity: transforms a complex URL (index.php?id=123) into a readable URL (/post-title/).
  • Referencing (SEO): search engines prioritize URLs containing keywords over technical parameters.
  • Security: hides the internal structure of your site and the technologies used (such as .php extensions).

 

URL rewriting example

Consider the URL: article.php?id=25&cat=4. The goal is for it to appear as: /article/25/4/. Here is how to configure your .htaccess file:

# Désactive l'affichage des répertoires et suit les liens symboliques
Options -Indexes +SymLinksIfOwnerMatch
# Active le moteur de réécriture
RewriteEngine on
# Définit la base de réécriture si votre site est dans un sous-dossier (ex: /admin/)
# RewriteBase /admin/
# Règle de réécriture
RewriteRule ^article/([0-9]+)/([0-9]+)/?$ article.php?id=$1&cat=$2 [L,QSA]
  • [L] (Last): indicates that this is the last rule to apply if the condition is met.
  • [QSA] (Query String Append): allows other potential parameters to be preserved at the end of the URL.

Important: implementing these rules does not automatically update your links; you must manually update the internal links in your HTML code or database to use the new format.

 

Redirect to a main domain (SEO)

To avoid duplicate content, it is imperative to redirect your secondary domains to your main domain in HTTPS.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domaine-secondaire.xyz [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domaine-secondaire.xyz [NC]
RewriteRule ^(.*)$ https://www.mon-domaine-principal.com/$1 [R=301,L]

The R=301 header tells search engines that the redirect is permanent, thus transferring the SEO "power" to the correct domain.


Has this FAQ been helpful?