Edit htaccess WordPress. Thanks to the .htaccess file we can create rules for our Apache webserver, that is, the web space where our site resides. Thanks to these rules we can improve some behaviors of our website and speed up the loading time as well. In this guide we will find out some basic parameters of the .htaccess file and I will also provide you with some rules to add to your website.
How can I Edit htaccess WordPress?
For edit htaccess WordPress the easiest way (for experienced users) is to access via ftp to your web space and look in the "public" folder for the .htaccess file located in the root (main root) folder.

Here's how Edit htaccess WordPress via ftp
Otherwise we can install a plugin to edit the .htaccess file, among the available plugins I recommend these:
Redirections for User Agent
These are the standard redirections to be used to signal our website address to users and Googlebots.
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [OR] RewriteCond %{HTTP_HOST} ^olddomain.com$ RewriteCond %{HTTP_USER_AGENT} Googlebot [OR] RewriteCond %{HTTP_USER_AGENT} msnbot [OR] RewriteCond %{HTTP_USER_AGENT} Slurp RewriteRule ^(.*)$ http://bcd.com/$1 [L,R=301]
Set the default homepage
This type of rule allows us to express the default page of our website.
#Specify a default homepage (index page) DirectoryIndex home.html
Restrict access to specific IPs
This rule is useful for restricting access to the site to only certain ip's, think of a website under development that can only be visible to certain internet connections such as the one of the person who is designing it and the one of the client.
#Permit access only to specific IPs deny from all allow from 64.11.219.110 allow from 210.44.45.54
Redirect all pages
Questa regola consente di reindirizzare tutte le pagine verso un nuovo dominio.
#Rredirect all pages from old-domain.com to newdomain.com Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^www.vecchio-dominio.com$ [OR] RewriteCond %{HTTP_HOST} ^old-domain.com$ RewriteRule ^(.*)$ http://www.nuovo-dominio.com/$1 [R=301,L]
Preventing access to folders
Very often the space web is also used to store files that go outside the Web site, with this command you will block access to certain folders.
#Prevenire il caricamento di sottocartelle. #Questo comando va inserito nel file .htaccess del dominio primario RewriteCond %{HTTP_HOST} ^primary.com$ [OR] RewriteCond %{HTTP_HOST} ^www.primary.com$ RewriteRule ^addon.com/?(.*)$ "http://www.addon.com/$1" [R=301,L]
Preventing access to subdomains
Thanks to this rule it is possible to block access to sub-domains of our website.
#Prevenire il caricamento del sottodominio. #Questo comando va inserito nel file .htaccess del dominio primario RewriteCond %{HTTP_HOST} ^subname.primary.com$ [OR] RewriteCond %{HTTP_HOST} ^www.subname.primary.com$ RewriteRule ^(.*)$ "http://www.addon.com/$1" [R=301,L]
Forcing access on non-www domain.
If we want "www" not to be visible on our website, we must use this rule.
#Non inserire mai www nel dominio. #Sostituisci 'example.com' con il tuo dominio RewriteEngine on RewriteCond %{HTTP_HOST} ^www.(([a-z0-9_]+.)?example.com)$ [NC] RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
Force access on domain with www
In contrast to the previous rule, this one allows the "www" to be made explicit in the link to our site.
#Inserisci sempre www nel dominio #Sostituisci 'example.com' con il tuo dominio RewriteEngine on RewriteCond %{HTTP_HOST} ^([a-z.]+)?example.com$ [NC] RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]
Forcing access under HTTPS
If our website uses the HTTPS secure protocol and if it is not working properly, we can resolve the situation using this rule.
1TP5Always use HTTPS for secure connections #Substitute 'www.example.com' with your domain as it appears on the HTTPS certificate RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Blocking traffic from specific referrals
Some times we notice a spike of visits on our website, many times these visits are not from real users but are generated from external referrals that many times are useless, thanks to this rule we can block every single referal by inserting it in this list; we only need to add the website with the related extension.
#Bloccare il traffico proveniente da certi siti RewriteEngine on Options +FollowSymlinks RewriteCond %{HTTP_REFERER} badsite.com [NC,OR] RewriteCond %{HTTP_REFERER} badforum.com [NC,OR] RewriteCond %{HTTP_REFERER} badsearchengine.com [NC] RewriteRule .* - [F]
Deny access to certain types of files
With this simple rule we can deny access to certain file extensions.
#Non permettere il caricamento di specifici tipi di file RewriteEngine on RewriteRule .*.(jpg|jpeg|gif|png|bmp|exe|swf)$ - [F,NC]
Enabling Caching
This rule enables Caching to increase the loading speed of the website.
# Add Caching. Header set Cache-Control "max-age=3600"