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.
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.
#PPrevent subdirectories from being loaded. #This command should be placed in the .htaccess file of the primary domain 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.
#PPrevent subdomain loading. #This command should be placed in the .htaccess file of the primary domain 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.
#Never put www in the domain. #SReplace 'example.com' with your domain name 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.
#B Blocking traffic from certain sites 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.
#Do not allow specific file types to be uploaded 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"
Enable compression of HTML, CSS, JavaScript, and XML files
Thanks to this rule it is possible to compress some files of our website. It is also possible to gain several seconds in the loading of our website.
# Compress text, html, javascript, css, xml: AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript
WordPress redirections after permalink change
This feature is useful in case our permalinks are replaced by new ones.
## From permalink "Day and Name (/%year%/%monthnum%/%day%/%postname%/)" to "postname" RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.yourwebsite.com/$4 ## From permalink "Month and Name (/%year%/%monthnum%/%postname%/)" to "postname" RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)$ http://www.yourwebsite.com/$3 ## From permalink "Numeric (/archives/%post_id%)" to "postname" RedirectMatch 301 ^/archives/(\d+)$ http://www.yourwebsite.com/?p=$1
Standard file configuration to edit htaccess WordPress
# BEGIN WordPress . RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L]. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]. # END WordPress ## ENABLE GZIP COMPRESSION ## AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript ## ENABLE GZIP COMPRESSION ## ## EXPIRES CACHING ## . ExpiresActive On ExpiresByType image/jpg "access 1 year" ExpiresByType image/jpeg "access 1 year" ExpiresByType image/gif "access 1 year" ExpiresByType image/png "access 1 year" ExpiresByType text/css "access 1 month" ExpiresByType application/pdf "access 1 month" ExpiresByType application/javascript "access 1 month" ExpiresByType application/x-javascript "access 1 month" ExpiresByType application/x-shockwave-flash "access 1 month" ExpiresByType image/x-icon "access 1 year" ExpiresDefault "access 2 days" ## EXPIRES CACHING ##