.htaccess for WordPress

For the correct operation of your site on CMS WordPress, a file such as .htaccess is required.

What is this animal and why is it needed on the site? This file is not included in the delivery of WordPress, probably because everyone configures it himself, but there are common, and I would say, important, necessary, general settings for the htaccess file for WordPress.

htaccess is primarily designed to protect your site from network attacks, and it also allows you to modify some of the settings of the server on which your site is located, without going into the hosting management panel to enter the server mode. The file itself should be located on your hosting in the root directory. By the way, it’s better to work with FTP .

htaccess wordpress file location

  • The htaccess file allows:
  • completely or partially block the site
  • block access by IP
  • block spam
  • configure redirect 301 (redirect from www to without www)
  • configure redirect – 302 ( temporarily moved)
  • set up redirect – 404 (file not found)
  • manage static data
  • increase the download speed of your site

Creating an htaccess file for WordPress

And so, if your server still has this file, then the first thing you need to create it. Make and later edit it can be in a regular notebook. As you have noticed, this file has no name and with the extension .htaccess. This is how you save the blank scratchpad document by specifying only the extension with a period.

Now you can do the setup. The standard settings include the following entry:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

By the way, the standard .htaccess file for WordPress can be downloaded here.

After adding this entry, you can proceed to additional settings, if necessary, they are carried out by adding new functions, commands to it.

Protecting the .htaccess file

Yes, it’s better to be safe 🙂

<Files .htaccess>
 order allow,deny
 deny from all

Protecting the wp-config.php file:

<Files wp-config.php>
 order allow,deny
 deny from all

IP Blocking

<Limit GET POST>
 order allow,deny
 deny from xxx.xxx.xxx.xxx
 deny from yyy.yyy.yyy.yyy
 allow from all

* where xxx and yyy are the ip addresses of the villains

Additional protection of the site from spam

Some spam bots refer to the wp-comments-post.php file directly, and this file is responsible for comments on yours. But you can catch such requests by checking all requests for REFERER, because it will have ordinary visitors, and spam bots are usually deprived of them. (for reference: Referer (HTTP referer) – one of the client request headers in the HTTP protocol.It contains the URL of the request source.

When moving from one page to another, the referer will contain the address of the first page.Only the HTTP server installs software that analyzes the referer and extracting various information from it.

For example, the owner of a website gets the opportunity to know by what search queries, how often and on which pages people get.)

 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
 RewriteCond %{HTTP_REFERER} !.*yourdomain.com.* [OR]
 RewriteCond %{HTTP_USER_AGENT} ^$
 RewriteRule (.*) http://%{REMOTE_ADDR}/$ [R=301,L]

Configure redirection from www to without www – redirect 301

This must be done. Often many people ignore this option.

 Options +FollowSymLinks
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^www.saaait\.ru$ [NC]
 RewriteRule ^(.*)$ http://saaait.ru/$1 [R=301,L]

Configure redirection without www on www – redirect 301

Options +FollowSymLinks
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^saaait\.ru$ [NC]
 RewriteRule ^(.*)$ http://www.saaait.ru/$1 [R=301,L]

Why do you need a 301 redirect?

The fact is that the search engines consider the same site with www and without it different sites. And if you are promoting the site, the reference weight can flow out due to this writing and not the correct setting. And all the external links to your site will be divided into two parts, depending on how the link is placed to your site (with or without www), and in the absence of a redirect, you will actually lose some of the links, as they will spread in the end to 2 different sites.

Delete / category / from the address bar By default,

categories in WordPress are displayed like this: http://urokwp.ru/blog/uroki-wordpress/htaccess-dlya-wordpress
And this is not very good, firstly a very long address is obtained, secondly, an extra nesting in the structure. Now we can correct this disgrace with the help of .htaccess:

1 RewriteRule ^category/(.+)$ http://learningtips.eu/$1 [R=301,L]

Now look like this: http://learningtips.eu/htaccess-for-wordpress/

Enable browser caching

This code improves the browser’s caching of static files. When you re-visit the site if the page code has not changed, then this page with all the content will be downloaded from the cache. There, by increasing the speed of the site.

 FileETag MTime Size
 <ifmodule mod_expires.c>
 <filesmatch "\.(jpg|gif|png|css|js)$">
 ExpiresActive on
 ExpiresDefault "access plus 1 year"

Gzip Data Compression

Compressing data on the site also allow you to increase the speed of your site’s download

 <IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
 BrowserMatch ^Mozilla/4 gzip-only-text/html
 BrowserMatch ^Mozilla/4.0[678] no-gzip
 BrowserMatch bMSIE !no-gzip !gzip-only-text/html
 <ifmodule mod_gzip.c>
 mod_gzip_on Yes
 mod_gzip_item_include file \.js$
 mod_gzip_item_include file \.css$ </ifmodule>

About the author

Add Comment

By admin

Recent Posts

Recent Comments