Creation and use of htaccess file on cPanel

Procédure

Creation and use of .htaccess file on cPanel

The .htaccess file is a configuration file that allows you to apply specific configurations to the web server. It is supported by several types of web servers, including the Apache web server which is provided on our cPanel web hosting plans.

The .htaccess files apply configurations at the directory level and all subdirectories within which the file resides.

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

Creation and use of .htaccess file on cPanel

Each folder (directory) of your cPanel hosting could have a .htaccess file. Generally, there is one at the root of your hosting, the folder: public_html

To create a .htaccess file in your cPanel hosting:

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

Creation and use of htaccess file on cPanel

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

Creation and use of htaccess file on cPanel

  • Then check the "Show Hidden Files (dotfiles)" option and click the "Save" button.

Creation and use of htaccess file on cPanel

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

Creation and use of htaccess file on cPanel

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

Creation and use of htaccess file on cPanel

  • A .htaccess file will be created in the folder, to edit it right-click using the mouse button and click on the "Edit" menu.

Creation and use of htaccess file on cPanel

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

Creation and use of htaccess file on cPanel

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

Creation and use of htaccess file on cPanel

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

Creation and use of htaccess file on cPanel

Here are some .htaccess directives that you might use:

Redirect a page on your site to another:

Redirect 301 /oldpage.php http://www.domain.com/newpage.php

Redirect an entire site to a new URL:

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

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

Redirect 301 / http://www.domain.com/folder/

Redirect a subfolder to another URL:

Redirect 301 /sub-folder http://www.domain.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 redirects with URL rewriting.

Redirect the old domain to the new one

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

Redirect to www.

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

Redirect to the www version in a subfolder

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

Redirect the old site to the new one while preserving 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 directory while preserving links:

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

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

Original URL example:

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

Desired URL:

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

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 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 allow search engine spiders to know which pages they should index.

Feel free to share your comments and questions!

Go Further

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

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

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 911 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 909 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