1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Use URL rewriting
This guide explains the principle of rewriting URLs on the fly.
Preamble
- Rewriting URLs on the fly is a technique to change the appearance of URLs on a web page without actually changing the resource path.
- This process is done by virtual redirections, transforming a visible URL into another more aesthetic, while keeping the initial destination invisible to visitors.
- This method is often used to make URLs simpler and readable, by masking dynamic page settings.
- In addition to improving the aesthetics for visitors, it is beneficial for SEO, as search engines generally prefer URLs without complex parameters.
Example URL Rewrite
Take the example of the URL: article.php?id=25&categorie=4&page=3
It can be rewritten in: article-25-4-3.html
or titre-article-25-4-3.html
Here's how to configure this in a file .htaccess
if article.php
is in the directory web/admin/
:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /admin/
RewriteRule ^article-([0-9]*)-([0-9]*)-([0-9]*).html$ article.php?id=$1&categorie=$2&page=$3 [L]
- Options +FollowSymlinks : authorizes the use of symbolic links
- RewriteEngine on : activates the URL rewrite module from Apache
- RewriteBase /admin/ : indicates the work directory
- RewriteRule : defined the rewrite rule
With this configuration, when a user accesses article-25-4-3.html
, it is redirected to article.php?id=25&categorie=4&page=3
without it being visible.
Even if the URL rewrite is in place, the old URL remains functional. It is therefore crucial to update all internal links your site to adopt the new URL format.
Redirect to another domain
If you have multiple domains pointing to the same site, you can redirect all queries to a main domain. P.e. if www.domaine.xyz
and www.mon-domaine.xyz
lead to the same site, but that www.mon-domaine.xyz
is your main domain, use this rule in the .htaccess
of the www.domaine.xyz
:
RewriteEngine On
RewriteRule ^(.*)$ http://www.mon-domaine.xyz/$1 [R=301]
This will redirect all pages of www.domaine.xyz
to www.mon-domaine.xyz
in a transparent manner, with permanent redirection (R=301
).
Also take note of this other guide on this subject.