Creating and using htaccess files on cPanel

Procédure

Creating and using .htaccess files on cPanel

The .htaccess file is a configuration file that controls how the web server responds to various requests. It is supported by many web servers, including the popular Apache web server used by most commercial web hosting providers.

.htaccess files operate at directory level, allowing them to override global configuration settings in .htaccess directives higher up in the directory tree.

In this tutorial, you will learn how to create and use an .htaccess file.

Creating and using .htaccess files on cPanel

In theory, every folder (directory) on your cPanel hosting could have an .htaccess file. As a general rule, there is one at the root of your hosting, the folder: public_html

To create an .htaccess file in your cPanel hosting:

  • Log in to your cPanel
  • Click on the "File Manager" icon.

Creating and using htaccess files on cPanel

  • If the display of system files is deactivated in cPanel, you can activate it by clicking on the "Settings" icon in the file manager.

Creating and using htaccess files on cPanel

  • Then tick the "Show hidden files (dotfiles)" option and click the "Save" button.

Creating and using htaccess files on cPanel

  • Next, to create the .htaccess file, from the File Manager toolbar, click on "+File".

Creating and using htaccess files on cPanel

  • A small window will open, type .htaccess in the"file name " field and click on the"Create New File" button.

Creating and using htaccess files on cPanel

  • An .htaccess file will be created in the folder. To edit it, right-click with the mouse and click on the"Edit" menu.

Creating and using htaccess files on cPanel

  • In the next window, click on the " Edit" button.

Creating and using htaccess files on cPanel

  • The file will open in the code editor, add the .htaccess directives you wish to use.

Creating and using htaccess files on cPanel

  • Save the file by pressing the"Save changes" button and close the window by clicking Close.

Creating and using htaccess files on cPanel

Here are some .htaccess directives you could use:

Redirect one page of your site to another :

Redirect 301 /oldpage.php http://www.domaine.com/nouvellepage.php

Redirect an entire site to a new URL:

Redirect 301 / http://www.domaine.com/

Redirect an entire site to a sub-folder, useful for redirecting the main site of your cPanel (published in "public_html") to the folder of an additional domain:

Redirect 301 / http://www.domaine.com/dossier/

Redirect a subfolder to another URL:

Redirect 301 /subfolder http://www.domaine.com/

This directive will redirect any file with the .html extension to use the same filename but use the .php extension instead.

RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
  • You can also do 301 redirect with URL rewriting

Redirect the old domain to the new one

RewriteEngine on RewriteBase / RewriteRule (.*) http://www.nouveaudomaine.com/$1 [R=301,L]

Redirect to www.

RewriteEngine on RewriteBase / rewritecond %{http_host} ^domaine.com [nc] rewriterule ^(.*)$ http://www.domaine.com/$1 [r=301,nc]

Redirect to the www. version in a subfolder

RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} domaine.com [NC] RewriteRule ^(.*)$ http://www.domaine.com/dossier/index.html [R=301,NC]

Redirect the old site to the new one, preserving the links :

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

Redirect the old domain contained in a folder to the new one, with or without a directory, preserving the links:

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_URI} ^/folder/(.*)$ RewriteRule ^(.*) http://www.nouveaudomaine.com/%1 [R=302,NC]

Redirect URLs with parameters (files placed in the root folder)

Example Original URL :

http://www.example.com/index.php?id=1

Desired URL :

http://www.example.com/nouvel-emplacement/

The .htaccess syntax :

RewriteEngine on RewriteCond %{QUERY_STRING} id=1 RewriteRule ^index\.php$ /new-location/? [L,R=301]

Redirect URLs with parameters (files placed in a subfolder)

Original URL :

http://www.example.com/sub-dir/index.php?id=1

Desired URL :

http://www.example.com/path-to-new-location/

The .htaccess syntax :

RewriteEngine on RewriteCond %{QUERY_STRING} id=1 RewriteRule ^sub-dir/index\.php$ /path-to-new-location/? [L,R=301]

Redirect the root of your website to the "public" folder, useful for the Symfony and Laravel frameworks:

RewriteEngine on RewriteCond %{REQUEST_URI} !^public RewriteRule ^(.*)$ public/$1 [L]

Conclusion

You have learned how to create and use your htaccess file in your cPanel hosting. The .htaccess file can be used to perform SEO-related tasks such as redirects. Redirects can be used to avoid 404 error messages and to let search engine spiders know which pages to index.

Don't hesitate to share your comments and questions!

Going further

You can discover other .htaccess directives on the following pages:

Rate this article :

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

1mn reading

How do I change my package to cPanel hosting?

1mn reading

Wordpress Cpanel: lost administration login

0mn reading

How do you migrate your website from LWS Panel to cPanel?

0mn reading

How to migrate from cPanel hosting to a VPS server


Questions sur l'article
shankly Il y a 355 days
ou puis je trouver le nom de mon serveur pour l'intégrer dans une page script php afin de me connecter à ma base de données. merci
See the
1 answers
Jordan-LWS - Il y a 353 days

Bonjour,

 

Merci pour votre retour.

Afin de pouvoir retrouver l'adresse de votre serveur SQL, l'idéal est de se rendre dans votre espace client LWS puis dans MySQL & phpMyAdmin, ici, vous devriez avoir la possibilité de créer une base de données ce qui entrainera un envoi de mail automatique avec un mot de passe et les informations de connexion à cette dernière. Vous pouvez également suivre cette documentation qui devrait vous renseigner d'avantages.

L'information du serveur est également affichée dans la liste des bases de données sous la colonne "IP Serveur"

Dans le cas où vous auriez une formule de type cPanel, une documentation est aussi mise à dispositions si vous le souhaitez. Il suffira d'utiliser l'adresse IP du serveur indiqué dans la partie droite de votre cPanel, elle est indiquée sous Shared IP Address.

Je vous remercie pour votre attention et reste à votre disposition pour toutes autres demandes ou interrogations complémentaires à propos de nos services. Vous pouvez nous contacter directement depuis votre espace client ou par le biais de cette page.

 

Cordialement, L'équipe LWS.

Utile ?

Ask the LWS team and its community a question