When you make use of the .htaccess file, you can essentially control several aspects of the Apache webserver as well as its multiple variants. This what is .htaccess file? guide will show you how to set up custom error pages, redirect, password-protect directories, and so much more.
What is the .htaccess file?
The .htaccess file refers to the configuration file that controls precisely how a webserver acutely responds to numerous requests. Multiple web servers generally support it, and this includes the very popular Apache web server that is primarily used by almost all commercial web-hosting providers.
In other words,what is .htaccess file? is a configuration file that occupies a particular directory and indicates which groups of users or users can be permitted or allowed access to the files contained inside that directory.
The .htaccess file generally functions at the level of a directory, thereby allowing them to prevail over global configuration settings of .htaccess directives that are much higher in the directory tree.
If you have installed a CMS (Content Management System) such as WordPress, Joomla, or Drupal, you have most likely encountered what is .htaccess file? You may not have to edit it, but it was one of the files that you had to upload to your web server.
By the way, its name – e.g. .htaccess – begins with a dot or period, and ends with ‘htaccess.’ If you attempt to edit it, you need to ensure that it stays that way. And you need to make sure it does not end up with a .html or .txt extension.
You should also take note that a few web hosts don’t allow you to edit the .htaccess file. But then, on most of these web hosts, you can create your very own .htaccess file and even upload it to specific or chosen directories. It will control those directories as well as the subdirectories below it.
.htaccess files are composed in the Apache programming language. You do not have to have in-depth knowledge or a full grasp of the language because it can be incredibly frustrating.
It is not only a very challenging programming language to learn, but it is also because it does not provide any descriptive errors at all. It will only prepare a page that will be styled the same as a server error.
One small mistake in a .htaccess can bring about a significant amount of downtime, especially if you cannot resolve it as quickly as possible. And for this reason, your nifty fingers need to be very close to the CTRL+Z keys and always keep backups if necessary.
Some features of the .htaccess file include the following:
- Ability to protect folders with passwords
- Stop directory listings
- Allow users or ban users making use of IP addresses.
- Redirect users to another directory or page automatically
- Create and use custom error pages
- Change the way files with some extensions are employed.
- Make use of a different file as the index file by specifying the specific file or file extension.
Why is it called .htaccess?
.htaccess file is better known as ‘hypertext access.’ This name is coined from the original use of the tool which is to control user access to specific files on a per-directory basis.
Making use of a subset of Apache’s ‘http.conf’ settings directives, .htaccess gave room to a system administrator to limit access to independent directories to users with names and passwords specified in an attendant .htaccess file.
Although .htaccess files are somewhat used for this, they are also utilized for several other things.
How is .htaccess used?
.htaccess can be used for the following:
- Enabling password protection for website pages or directories
- Permanently or temporarily redirect visitors from one page to another.
- Prevent other web developers from directly making use of your images in order not to pay the bandwidth, also known as hotlinking
- Boosting SEO via consistent trailing slash policies
- Inform people’s web browsers of the expiration dates of your web files, so they do not keep downloading them for no particular reason (cache-control)
- To prevent some folders from getting searched openly by visitors.
- Make use of a different file as the index file by specifying the specific file of the file extension.
- Stop directory listings
- Force your website to use HTTPS instead of HTTP
- Create and display custom error pages such as 404 pages, etc.
When it comes to improving SEO via a consistent trailing slash policy, you – as the webmaster – can choose to have a trailing slash at the very end of each URL on a website or not.
Bear in mind that the .htaccess file is an easy and swift way to modify or alter Apache’s core configuration. This implies that the core configuration file decides what can be tweaked right from the .htaccess file.
There is a well-known downside in making use of what is .htaccess file?: it slows down Apache and therefore impedes your web visitors’ experience. This is because it is a file that has to be read in each visit. Moreover, it can make your folder hierarchy somewhat crazy to control and update.
The following are also crucial to keep in mind:
- In general, you should reduce the number of .htaccess files on your web server. This is because having too many configurations on that server can significantly slow it down.
- .htaccess files that are somewhat higher up in the file path perpetually take precedence
- When your web server sees a what is .htaccess file? inside a directory, it starts looking for .htaccess files in all the parent directories as well. And this considerably increases server resource usage.
Where can you find the .htaccess file?
Theoretically, every directory on your server could have one .htaccess file. As a rule, though, there will be one in your webroot folder – i.e. the folder that holds the entire content on your site – and is commonly labeled with something like ‘public_html’ or ‘www.’
If you have only a single folder or directory that contains several website subdirectories, there will generally be a .htaccess file located in the main root – i.e. public_html – directory and also one in every subdirectory, i.e. /sitename.
But I can’t find my .htaccess file
So, what if you can’t find yourwhat is .htaccess file? you ask? What you may not know is that on the majority of the file systems out there, file names usually begin with a dot. And they are hidden files, meaning that they are generally not visible in by default.
However, they are not really all that hard to get to. Your File Manager or FTP client should showcase a setting for ‘show hidden files.’ This can be in different places and other programs. However, they should be in ‘Settings,’ ‘Folder Options,’ or ‘Preferences.’ At times, you may find it in the ‘View’ menu.
What if I do not have a .htaccess file?
The first thing you need to do is switch on ‘show hidden files’ – or its equivalent. This is to ensure that you don’t actually have one. In most cases, .htaccess files are automatically created, so you will generally have one. But this is not always the case.
If you discover that you don’t have one, you can easily create it by following the steps outlined below:
- Create a plain text editor. This will be a new file.
- Save the plain text editor in ASCII format – and NOT UTF-8 or any other format – as .htaccess.
- Be sure that the plain text editor is not saved as ‘.htaccess.txt’ or something like it. The file should have a single name, i.e. .htaccess with no additional file extension.
- Upload the file to its appropriate directory via your browser-based file manager or FTP.
Error code: What is It?
One primary use of the .htaccess file is to redirect any user to a custom error page, though this depends on the particular web server error they encounter.
By making use of a custom error page, you can present them along with a list of your website’s top-performing articles as well as a sitemap with hyperlinks to the other areas of your website. And it can even include your website’s navigation system.
It can feature an FAQ page so visitors who are searching for relevant or specific information on your site but cannot find it, can narrow down the location of that particular information on your website without clicking away, going back to Google or any other search engine of their choice, and most likely not returning ever again to your website.
When a user sends a request to a web server, the latter tries to respond swiftly to that request. This occurs by delivering a document – if it has to do with HTML pages – or via accessing an app and returning the output when it has to do with CMS and other web applications.
If anything goes wrong, it generates an error. Different types of errors show up with various error codes. You are likely familiar with the usually frustrating 404 error which is caused if a document can’t be found on the server.
There are several other error codes that any server can respond with. Here they are in no particular order:
Server Errors
- 500: Internal Server Error
- 501: Not Implemented
- 502: Bad Gateway
- 503: Service Unavailable
- 504: Gateway Timeout
- 505: HTTP Version Not Supported
Client Request Error
- 400: Bad Request
- 401: Authorization Required
- 402: Payment Required (not used yet)
- 403: Forbidden
- 404: File Not Found
- 405: Method Not Allowed
- 406: Not Acceptable (encoding)
- 407: Proxy Authentication Required
- 408: Request Timed Out
- 409: Conflicting Request
- 410: Gone
- 411: Content-Length Required
- 412: Precondition Failed
- 413: Request Entity Too Long
- 414: Request URI Too Long
- 415: Unsupported Media Type
How to edit your .htaccess file
If there is a need to edit your .htaccess file, you may have to download the existing file by making use of your FTP client or through your host’s file system, e.g. within cPanel.
Your site’s .htaccess file may already comprise of vitally important settings. If your .htaccess file has existing settings, add the new code to whatever it is you already have there.
Please, take note: don’t delete the existing .htaccess file unless it is empty of you are 100 percent sure it is okay to do away with it.
Error Handling (default)
If you do not specify or define any type of error handling, the server will return the message to the browser. And the browser will then display a generic error message to you, the user. This, of course, is not generally ideal.
Specifying Error Documents
To specify error documents, start by creating an HTML document for every error code that you want to handle. Name them whatever you like; however, naming them with something you can easily recall would be helpful.
In the .htaccess file, specify which document to utilize with each type of error. E.g.
ErrorDocument 400 /errors/bad-request.html
ErrorDocument 401 /errors/auth-reqd.html
ErrorDocument 403 /errors/forbid.html
ErrorDocument 404 /errors/not-found.html
ErrorDocument 500 /errors/server-err.html
Did you notice that each directive is placed on its own line? That is the way it should be.
Redirecting URLs with the .htaccess file
If you make up your mind to change the organization of your website or domain names, you can easily set up URL redirects in order to ensure your web visitors are not confused. And one of the easiest ways is via .htaccess files.
In order to edit the .htaccess file, you will have to re-create the file by yourself and then upload it to the server using SFTP. Follow the steps outlined below:
Step 1: Log into your web server via SFTP.
Step 2: Locate and then download your .htaccess file. You will generally find it in your content folder. If you do not have a .htaccess file, you can easily create a file called ‘.htaccess’ on your computer in order to hold your configurations.
Step 3: Open up the file using your favorite text editor
Step 4: Add the following lines to your file:
# enable basic rewriting
RewriteEngine on
Step 5: Copy and then past the new configurations into the .htaccess file and then save the file.
Step 6: Upload this back to the directory you initially took your .htaccess file from
Step 7: Test your work by visiting your site and then viewing the pages you want to redirect.
You can also make use of the what is .htaccess file? to redirect independent pages on your site using 301 and even 302 redirects.The .htaccess file is one of the most powerful tools you can utilize for on-site SEO. It is highly essential for any webmaster – that knows their onions – to be aware of this file and can do the needful when it is crucial.