How do I create an htaccess file to rewrite a URL?

Procédure

What is URL rewriting?

URL rewriting is a complex but very important subject for a website. The aim of URL rewriting is manifold:

  • To improve your website's referencing
  • Simplify navigation on your site
  • Allow your site to evolve
  • Manage redirects

Practical case of URL redirection using htaccess

How to create a simple redirect?

RewriteEngine On RewriteRule .* testing.php

This code will redirect all requests to the page "testing.php".


How to redirect yourdomain.com to www.yourdomain.com?

RewriteEngine on Rewritecond %{HTTP_HOST} ^exemple.com$ Rewriterule ^(.*) http://www.votredomaine.com/$1 [QSA,L,R=301]


How can I redirect to another folder without displaying the redirection folder?

You may have moved your website to another FTP folder and you don't want your visitors to know about this new folder. This will force the redirect to be www.votredomaine.com when in fact it should be www.votredomaine.com/dossier.

RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} /MyFolder/([^\s?]*) [NC] RewriteRule ^ %1 [L,NE,R=302] RewriteRule ^((?!MyFolder/).*)$ MyFolder/$1 [L,NC]


URL rewriting

The mod_rewrite module can be used to rewrite URLs.

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !NondeLaPage.php RewriteRule .* NondeLaPage.php

Controlling access to files

An .htaccess file is often used to restrict or deny access to files and folders. To do this, proceed as follows:

deny from all

However, you can deny access to a specific IP address using the following directives:

order allow,deny deny from XXX.XXX.XXX.XXX allow from all

or several IP addresses if you wish:

allow from all deny from 145.186.14.122 deny from 124.92.14.086

Setting up a redirect for tablets and smartphones

You can redirect tablet and smartphone users to a specific web page or directory using the following directives:

RewriteCond %{HTTP_USER_AGENT} ^.*iPad.*$ RewriteRule ^(.*)$ http://votredomaine.com/dossier [R=301] RewriteCond %{HTTP_USER_AGENT} ^.*Android.*$ RewriteRule ^(.*)$ http://votredomaine.com/dossier [R=301]

Forcing a document to be saved

If you want to force users to download files rather than display them in the browser, you can use the following directives:

AddType application/octet-stream .csv AddType application/octet-stream .xls AddType application/octet-stream .doc AddType application/octet-stream .avi AddType application/octet-stream .mpg AddType application/octet-stream .mov AddType application/octet-stream .pdf

You can also put everything on the same line, as follows:

AddType application/octet-stream .avi .mpg .mov ;pdf .xls .mp4

Disable or enable directory browsing

# Disable directory browsing Options All -Indexes # Enable directory browsing Options All +Indexes

Change Charset headers and language.

You can change the Charset and language using the following directives:

AddDefaultCharset UTF-8 DefaultLanguage en-GB

Caching with mod_expires

Apache's mod_expires module allows you to define expiry intervals for different types of content on your website. For example, you can use mod_expires directives to tell browsers to cache image files for one hour, Javascrpt files for a fortnight and CSS files for two months.


     ExpiresActive On ExpiresByType image/png "access 1 hour" ExpiresByType image/gif "access 1 hour" ExpiresByType image/jpeg "access 1 hour" ExpiresByType text/javascript "access 2 weeks" ExpiresByType text/css "access 2 months" ExpiresByType text/html "modification 4 hours" ExpiresDefault "access 2 days"

Rate this article :

2.5/5 | 13 opinion

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

5mn reading

How do I create and use an .htaccess file with the LWS Panel?

0mn reading

How to configure SMTP on a CMS

0mn reading

Your contact form doesn't work and no email is received?

0mn reading

How do I update Wordpress to the latest version?


Ask the LWS team and its community a question