1000 FAQs, 500 tutorials and explanatory videos. Here, there are only solutions!
Use URL rewriting
This guide explains the principle of on-the-fly URL rewriting.
Introduction
- URL rewriting on the fly is a technique that allows you to change the appearance of a web page's URLs without actually altering the resource's path.
- This process is done through virtual redirects, transforming a visible URL into a more aesthetically pleasing one, while keeping the initial destination invisible to visitors.
- This method is often used to make URLs simpler and more readable, by hiding the parameters of dynamic pages.
- In addition to improving aesthetics for visitors, it is beneficial for search engine optimization, as search engines generally prefer URLs without complex parameters.
Example of URL rewriting
Consider the example of the URL: article.php?id=25&categorie=4&page=3
. It can be rewritten as: article-25-4-3.html
or titre-article-25-4-3.html
. Here's how to configure this in a .htaccess
file if article.php
is located in the web/admin/
directory:
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 : allows the use of symbolic links
- RewriteEngine on: enables the Apache URL rewriting module
- RewriteBase /admin/ : specifies the working directory
- RewriteRule: defines the rewrite rule
With this configuration, when a user accesses article-25-4-3.html
, they are redirected to article.php?id=25&categorie=4&page=3
without this being visible.
Even if URL rewriting is in place, the old URL remains functional. It is therefore crucial to update all internal links of your site to adopt the new URL format.
Redirect to another domain
If you own multiple domains pointing to the same site, you can redirect all requests to a main domain. For example, if www.domaine.xyz
and www.mon-domaine.xyz
lead to the same site, but www.mon-domaine.xyz
is your main domain, use this rule in the .htaccess
of www.domaine.xyz
:
RewriteEngine On
RewriteRule ^(.*)$ http://www.mon-domaine.xyz/$1 [R=301]
This will redirect all pages from www.domaine.xyz
to www.mon-domaine.xyz
transparently, with a permanent redirect (R=301
).
Also, refer to this other guide on this topic.