This article will be useful to those who have lost the default .htaccess file from the WordPress site or to those who wants to create right .htaccess file for different purposes. It is necessary if you use Apache web server.
When you download and install WordPress on an Apache server, it should already come with an .htaccess file. But since this file is hidden, you must enable the display of hidden files on your system. We have a detailed guide on how to find the .htaccess file. However, in some cases, this file may be missing, or be accidentally deleted. In this situation, we recommend creating the .htaccess file manually.
When talking about the best blogging system, WordPress always comes out on top. Despite the fact that WordPress most often acts as a Content Management System (CMS), his main function remains to manage a blog. In today’s guide, we will try to explain the meaning of the WordPress .htaccess file and show how to create it(also, you can check our WordPress Pricing table plugin).
The steps below will show how to create a file in test hosting. However, the sequence of actions should not differ much from your cPanel. You can also create a file on your computer and then upload it using an FTP client.
How to create WordPress default .htaccess file
To create an .htaccess file, simply navigate to the root directory of your WordPress site. This is usually the public_html directory. Next, create a text file and name it .htaccess.
Open the file with text editor. The .htaccess file contains several lines of code that are standard server settings. In WordPress, .htaccess should look something like this:
# BEGIN WordPress
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Just copy this code and paste it into the created .htaccess file. Save changes and exit.
You have created your own .htaccess file. Now visit your website and check if everything is working correctly, as incorrect file configuration may result 500 internal server error(also, check the – Best WordPress eCommerce plugins).
The .htaccess file is required for WordPress to work properly. It can provide several useful functions for the server, especially those related to security.
Now that you have gained a basic understanding of the important .htaccess file for WordPress, you can continue to explore its potential for improving your WordPress project.
What is the .htaccess file?
If you are a newbie developer, there is a possibility that you are not very familiar with the .htaccess file. There is a reason for this, this file is hidden by default and is located in the root directory of your site. If this file is there at all, which is not rare.
This file can be used for any type of website. Any site that is located on the Apache server can use the .htaccess file. .Htaccess itself is a server configuration file. When you start your site, the server searches for this file and, if it is in the directory of your site, executes it.
The purpose of the .htaccess file is to change certain settings of the Apache server. Therefore, it is very useful to enable and disable some server functions. For example, it is used to create a redirect from an address without www to www and vice versa. Another application is to change permissions for certain files, block bots, or block IP’s. It can also be useful for enhancing WordPress security. This file is very useful when working with WordPress, since you can change the settings to suit your needs(also, check this useful post – WordPress robots.txt).
Correct .htaccess for WordPress site
Here is the code for the improved .htaccess file for WordPress, you can pile up a lot of things in it, but this is the basis, which you must put instead of the standard one:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule (.+)/feed /$1 [R=301,L]
RewriteRule (.+)/attachment /$1 [R=301,L]
RewriteRule (.+)/comment-page /$1 [R=301,L]
RewriteRule (.+)/comments /$1 [R=301,L]
RewriteRule (.+)/trackback /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<Files wp-config.php>
# Deny access to the file wp-config.php
order allow,deny
deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files>
# END WordPress
Well, now let’s look at all parts of the code that we added to the standard version of the file.
This part of the code is responsible for 301 redirects from duplicate pages, so as not to spray weight and don’t have any problems with google(also, check the – WordPress scroll plugin):
RewriteRule (.+)/feed /$1 [R=301,L]
RewriteRule (.+)/attachment /$1 [R=301,L]
RewriteRule (.+)/comment-page /$1 [R=301,L]
RewriteRule (.+)/comments /$1 [R=301,L]
RewriteRule (.+)/trackback /$1 [R=301,L]
This part is responsible for closing the listing of files and folders on the server:
Options +FollowSymLinks -Indexes
Protecting the wp-config.php file from unauthorized access so as not to steal data from our database:
<Files wp-config.php>
# Запрещаем всем доступ к файлу wp-config.php
order allow,deny
deny from all
</Files>
This part of the code is responsible for protecting the htaccess file itself, as mentioned above from unauthorized access:
<Files .htaccess>
order allow,deny
deny from all
</Files>
In general, it’s everything that we wanted to tell and show today. Thanks for reading, share with your friends if you like this post.