Knowledge base
1000 FAQs, 500 tutorials and instructional videos. Here, there are only solutions!
Use URL rewriting
This guide explains the principle of URL rewriting and shows how to use it on an Infomaniak Web Hosting service using a .htaccess file.
Introduction
- URL rewriting allows you to present more readable and explicit addresses, for example by replacing a URL such as
index.php?id=123with/article/123/. - Clear URLs make it easier for visitors to navigate and can help improve natural search engine optimization (
SEO) by highlighting the content of the page. - This technique also makes it possible to hide certain technical details of the application, such as file extensions or the use of parameters in the URL, without changing the way the site works.
Example of URL rewriting
The following example transforms a URL such as article.php?id=25&cat=4 into a more readable URL in the form /article/25/4/.
Add the following rules to the .htaccess file located in the root directory of your site:
# Disable directory listing and allow symbolic links owned by the same user
Options -Indexes +SymLinksIfOwnerMatch
# Enable the rewrite engine
RewriteEngine On
# Set the rewrite base if your site is installed in a subdirectory
# RewriteBase /admin/
# Rewrite rule
RewriteRule ^article/([0-9]+)/([0-9]+)/?$ article.php?id=$1&cat=$2 [L,QSA]The options used in this rule have the following meaning:
L(Last): indicates that no other rewrite rule should be applied if this one matches.QSA(Query String Append): preserves any additional parameters that may already be present in the URL.
Important: Creating these rules does not automatically update the links on your website. You must update your internal links so that they use the new URL format.
Redirect to a primary domain
If multiple domain names point to the same website, it is recommended to redirect all secondary domains to a primary domain. This practice avoids duplicate content and allows search engines to index only one version of your website.
Example of a permanent redirect to a primary domain using 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 option indicates that this is a permanent redirect. Browsers and search engines are thus informed that the content is now accessible at this new address.
Link to this FAQ: https://faq.infomaniak.com/206
Has this FAQ been helpful?