Let’s learn everything related to the wordpress htaccess file from its definition and steps to create it in WordPress and how to edit it using cPanel
The .htaccess file is a sensitive file and has a significant impact on the site, and any error in writing the code may lead to the entire site stopping, so it is necessary to make a backup copy of the file before starting to modify it.
What does the htaccess file do?
htaccess file for WordPress belongs to the Apache server . Through this file, it is possible to control the management of the server settings of your website, and by using it, you can cancel the settings, in addition to activating or stopping the work of various functions and features.
As we note that the name of the main file is followed by a period. This means the period (.) that precedes the .htaccess file name indicates that this file is a hidden file.
htaccess file properties wordpress
- This file is used to process permalinks within pages.
- Rewrite the URL and redirect it.
- Prevent hotlinking in photos.
- Handling server errors.
- Password protection.
- Prevent access to wp-content folder
- Block browsing of files
- تعطيع Hotlinking
- Block access to certain IP addresses
- Enable browser cache
- Enable zip compression
- Redirect from one domain to another
Below you will find an explanation of some of the most important previous features and how to write each code for each of them.
create htaccess file wordpress
When you install WordPress, this file is installed and created automatically, but it can be deleted at any time, and the common problems that require deleting this file may be summarized in some of them:
- There is an error in the permanent site links
- There was an error reading the site files
- There is an error or problems with the issue of (cache files) on the site’s cache
- Having problems reading the default language of the site
Before you start, we recommend that you make a backup copy of the site, if you do not know how to backup a WordPress site, see the lesson here .
You will find the file when you log into the hosting root in your account and follow the following path:
public_html and then create a new file here and give it the following name .htaccess
And do not forget to put the point – it is preferable to copy the word as it is and paste it in the place of the name of the file that you created.
Now in the file you created, paste the following code:
# BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
After that, click on Save, then go to the WordPress control panel, and click on the permanent links section, in the WordPress settings icon, and here you will find options, including the “Save All” button, without changing any other option, just click on “Save All”.
You can add any code you want after #END WordPress or before #BEGIN WordPress in order to avoid mixing custom commands with the commands that WordPress writes by default on the .htaccess file, before starting the modifications it is recommended to make a backup copy of the file.
Protect wp-config.php
<files wp-config.php> order allow,deny deny from all </files>
Block browsing of files
Options All -Indexes
Enable browser cache
<IfModule mod_expires.c> ExpiresActive on ExpiresDefault "access plus 1 month" # CSS ExpiresByType text/css "access plus 1 year" # Data interchange ExpiresByType application/json "access plus 0 seconds" ExpiresByType application/xml "access plus 0 seconds" ExpiresByType text/xml "access plus 0 seconds" # Favicon (cannot be renamed!) ExpiresByType image/x-icon "access plus 1 week" # HTML components (HTCs) ExpiresByType text/x-component "access plus 1 month" # HTML ExpiresByType text/html "access plus 0 seconds" # JavaScript ExpiresByType application/javascript "access plus 1 year" # Manifest files ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds" ExpiresByType text/cache-manifest "access plus 0 seconds" # Media ExpiresByType audio/ogg "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType video/mp4 "access plus 1 month" ExpiresByType video/ogg "access plus 1 month" ExpiresByType video/webm "access plus 1 month" # Web feeds ExpiresByType application/atom+xml "access plus 1 hour" ExpiresByType application/rss+xml "access plus 1 hour" # Web fonts ExpiresByType application/font-woff2 "access plus 1 month" ExpiresByType application/font-woff "access plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month" ExpiresByType application/x-font-ttf "access plus 1 month" ExpiresByType font/opentype "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" </IfModule>
Hotlinking
RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www.)?your-domain.com/.*$ [NC] RewriteRule .(gif|jpg)$ http://www.your-domain.com/hotlink.gif [R,L]
Block access to certain IP addresses
order allow,deny deny from xxx.xxx.xx.x allow from all
Enable zip compression
<IfModule mod_deflate.c> # Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/xml # Remove browser bugs (only needed for really old browsers) BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html Header append Vary User-Agent </IfModule>
You can watch the following video to know the characteristics of this file if you do not like to read
