18 March, 2023

What Is the .htaccess File? How to Use and Edit It

Quick summary: The .htaccess (HyperText Access) file is a configuration file of the Apache web server. It works like a controller that sets how the files in a directory are accessed. It helps administrators strengthen website security, create URL redirects, block attacks, deny access by geographic region and manage user permissions without restarting the web server.

What is the .htaccess file? how it works and related useful information are essential when developing a website to meet practical needs, and there is a lot to learn and a lot of knowledge to cover. Having the basics helps us pick up techniques to use the .htaccess file better and more effectively. Let's MONA Media learn about all of this in this article.

What is the .htaccess file?

The .htaccess file (HyperText Access) is a configuration file on Apache web sever that is widely used on WordPress websites. Put simply, .htaccess is a file that controls how files in a specific directory are accessed.

It is known as a powerful tool for improving website securityand performance on WordPress as needed. With a few small edits you can create redirects, block attacks, deny access from certain regions of the web, block users, and more.

The .htaccess file is a powerful tool for improving security

The .htaccess file is a powerful tool for improving security

How to use .htaccess?

To work with the .htaccess file you need any editor that supports code text, such as PHP Designer or NetBeans, or even the Notepad that comes with Windows.

The .htaccess file is not written in any programming language. It is built from Regular Expression rules. So without the necessary knowledge, working with .htaccess files is difficult and takes more time and effort.

Also, even a very small mistake in the file can make the website show a 500 error. In effect, you have just broken your own website. So be extremely careful when editing the .htaccess file, and never forget to keep a backup before you touch it.

This file is usually located right inside the root directory of the hosting, where it is easy to see. Using it is simple and mistakes are kept to a minimum.

Notes when using .htaccess

When using .htaccess, make sure the file is kept as secure as possible so bad actors can't exploit it to harm your website. To guard against problems, it is recommended to save a copy of the .htaccess file before editing.

If users don't know the file well, or make even the smallest slip during configuration, your website may stop working or no longer work as well as before. So saving a copy of the .htaccess file needs great care.

What the .htaccess file does

The .htaccess file has its own uses and benefits. The main ones are:

uses of the .htaccess file

The .htaccess file has its own uses for WordPress websites

  • As one of the CMS largest and most widely used platforms today, WordPress attracts hackers. So keeping the site safe, at the highest possible level, is always an admin's top priority. You don't have to be a coder or developer to use the .htaccess file to add security to your website. This is its biggest and most important benefit.
  • In addition, being able to handle logic for any request without reloading or restarting the web server is also a benefit not to be overlooked.
  • With the .htaccess file, users who are not administrators can also make adjustments without affecting the whole APACHE configuration.
  • The .htaccess file is also valued for making it easy to set access permissions for users and ensure they can only access their own folder.
  • The .htaccess file also suits content management systems.

Basic .htaccess configuration guide

Configuring the .htaccess file takes a few simple steps, and there are several ways to do it. The 3 main ways are:

Method 1

  • Step 1: Open a text editor on Windows. You can use Notepad, then type the sample code below, paste it into the .htaccess file and save. The code is:

# BEGIN WordPress

RewriteEngine On

RewriteBase /

RewriteRule ^index.php$ – [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

# END WordPress

  • Step 2: The file is then uploaded to the public_html directory if the host is DirectAdmin. This is not an official method, but it is simple and can be used if you don't know the other methods.

Method 2

  • Step 1: Log in to your hosting account provided by your hosting provider, using your username and password.
  • Step 2: Right-click an empty area in File Manager and choose New File.
  • Step 3: Finally, enter the name .htaccess and click Create to save, and the file is created as required.

Method 3

  • Step 1: Access a Linux system and create a .htaccess file.
  • Step 2: At this step, copy the created file to Windows, or use a Linux hosting account to create the file and download it.
  • Step 3: Compress the file before downloading, and extract it fully after the download succeeds.
Configuring the .htaccess file

Configuring the .htaccess file

How to edit the .htaccess file

The .htaccess file is present by default when WordPress is installed, and is usually in the root directory of this CMS. It is a single file with no other files or any other extension.

Choosing the .htaccess file

Choosing the .htaccess file

Just open it with Notepad, Notepad++ or similar software. One special requirement: never add code to, or edit, the area between #BEGIN and #END. When you need to, the best approach is to add the new block below it. When editing, keep in mind:

Adding code to .htaccess

Adding code to .htaccess

  • After adding a block of code, make sure you save it.
  • If you insert several different blocks of code, don't add them all at the same time.
  • Adding them one by one lets you tell where an error is, so fixing it is faster and simpler.

Finding the .htaccess file on hosting with cPanel

For users on Web Hosting with cPanel as well, finding the .htaccess file is very easy. A few simple steps will do:

  • Completing "log in" to your hosting account, then click “File Manager” in the Files section. You will see the .htaccess file right inside the public_html directory.
Click File Manager to find the .htaccess file in cPanel

Click File Manager to find the .htaccess file in cPanel

  • If you don't see it, click “Setting” in the top right corner of the screen, then click “Show Hidden File”.
Choose Show Hidden Files

Choose Show Hidden Files

  • If your hosting doesn't have the file, you need to create one. Click the icon "+ File in the top left corner", or left-click an empty space and choose “New File”. Then "enter the file name .htaccess" and click “Creat New File” to finish.
Click Create New File to finish

Click Create New File to finish

When developing a website with WordPress, learning about file .htaccess is essential. We hope the useful information above gives you more knowledge you can apply when developing and managing a WordPress website for real work needs.

FAQ

What is the .htaccess file used for?
The .htaccess file configures the Apache web server at the directory level: it sets up URL redirects, strengthens website security, blocks attacks, blocks users or denies access from certain regions, and controls user access permissions, all without touching the whole APACHE configuration.
What do you edit the .htaccess file with?
You can edit the .htaccess file with any text editor, such as Notepad (Windows), PHP Designer or NetBeans. Back up the file before editing, because even a small mistake can cause a 500 error and make the website inaccessible.
About The MONA Group: Founded in 2016, with 60+ staff, MONA has delivered 14,000+ projects with an 85% client retention rate. MONA provides website design, full-scope SEO, custom software and AI automation solutions for businesses. Consultation hotline: 1900 636 648. Explore MONA AI Agent suite and MONA SEO services.
MONA has a podcast: listen instead of readingEpisodes on AI, automation & SEO for Vietnamese businesses
Nghe ngay
About the author

CEO & Founder · The MONA

Founder of The MONA, who built a services company of 200+ staff with 14,000+ projects delivered. Khánh Hùng shares how he thinks about building systems that run themselves, automating a business, and the real lessons from delivering projects at MONA.

Views 1,5K
Rate this article 4,9/5 · 16 ratings

Professional website
design services

Own a website with a beautiful, 100% exclusive design and a lifetime warranty, with the ability
to expand features flexibly as your business grows, starting today!

Contact Mona

    MONA có riêng một Người "Bạn Thân" cho bạn - Người Account sẽ đồng hành, hỗ trợ, hướng dẫn, đặt đồ ăn cho bạn mãi mãi, từ đây về sau!
    MONA will never sell your information or send you spam
    Đội MONA tư vấn hệ thống quản lý giáo dục trực tuyến
    Hỏi đáp giáo dục 4.0
    Tạo cuộc hẹn miễn phí với MONA để giải đáp và tư vấn mọi thắc mắc về giải pháp số hoá ngành giáo dục
    Thời lượng cuộc hẹn
    45 Phút
    Ngày và giờ
    Thứ 2, ngày 25 tháng 12, 2023
    [9:30 - 10:15]

      Chọn ngày và giờ
      Khung giờ
      Back
      Hãy cho MONA biết bạn là ai
      0:00