Marketing and Business Intelligence

Optimize htaccess SEO WordPress

Picture of Julián Sánchez
Julian Sanchez
Know the best htaccess rules SEO Wordpress to work on your projects. Improve the SEO and fix bugs in htaccess.
wordpress htaccess seo

Carrying out a correct configuration of the Apache .htaccess file can help us speed up tasks, correct errors and even optimize part of the work SEO in our web positioning or maintenance projects. Today we tell you rules of ..htaccess SEO WordPress that you should know.

With the addition of rules in this file we can add an extra layer of security protection, such as preventing hotlinking, protecting WordPress (WP) directories and files, controlling bandwidth theft or compressing files.

It is very important to know how delicate this file is to handle since it controls the loading of the web page on the server. That is why we recommend make a backup or even download a copy of this file before editing it.

En Millennials Consulting we have the best team of experts SEO and they can help you with any topic related to the positioning of your website.

Basic list of Htaccess rules SEO WordPress

These simple rules will save you a lot of problems in web migrations or changes in web architecture.

1. Remove the "www" in all domain urls

#remove www from URI
Rewrit eEngine On
Rewrit eCond %{HTTP_HOST} ^$ [NC]
Rewrit eRule ^(.*)$$1 [L,R=301]

2. Remove file extension from a URL

Very useful to create beneficial friendly urls in SEO. Example: – >

Rewrite Rule ^(([^/]+/)*[^.]+)$ /$1.php [L]

3. Remove a string from a URL

This rule is very useful to remove the well-known “category” from urls:> However, if we can configure it in WP, much better. When we start a project we must configure the structure of the permanlinks.

Rewrite Rule ^category/(.+)$$1 [R=301,L]

4. Enable GZIP compression

This rule will help us to load the web faster.

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

5. 301 redirection from the old domain to a new one

Very useful rule to make a web migration or change of domain without losing traffic. In addition, with the use of rule 301 you will not incur a penalty SEO for duplicate content. If our domain was on another server we should only keep the file .htaccess is the old server for the redirect to take effect.

Rewrit eEngine On
Rewrit eRule ^(.*)$$1 [R=301,L]

6. Add trailing slash to the url

This is very useful to avoid duplicate content. For example, if our server serves urls like this and for Google are 2 different urls but we offer the same content.

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.)/$ RewriteRule (.)$1/ [L,R=301]

7. 301 redirects by url and the rest to the home page

Through this rule we can redirect certain URLs to a new domain and the rest to the home page. In this case, the order of the rules is important, from top to bottom in order of preference.

# primero redirige las url objetivo y el resto a la home
Redirect /dierproefvrije-cosmetica-not-tested-on-animals/
Redirect /bad-douche/
Redirect /gezichtsverzorging/
# el resto de urls a la home
redirectMatch 301 ^(.*)/$

8. Redirect domain and other urls

With this rule we can redirect all urls to the new domain.

redirectMatch 301 ^(.*)/$$1

9. Friendly 301 Redirects SEO

If you want to redirect a specific page without affecting the SEO you can use this rule.

Redirect 301 /d/file.html

10. Redirects to custom pages for each type of error

With this simple rule we can redirect each 40X error to a specific url.

ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php

11. Force cache expiration

This rule helps us load the page faster when the same user visits the web again.

FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus 86400 seconds"


Contact us to talk about your business. Our technicians and professionals will teach you the possibilities of improving your web positioning.

List of htaccess rules for security

With these rules you will be able to increase the security in your WordPress.

12. Add security headers

HTTP headers inform the web browser of how to act during its interaction with the website. These headers consist of metadata that provide information to the browser. Using the correct response headers we can strengthen security of the web.

For example, we can add strict transport security to force browsers to communicate with your website only over HTTPS.

Some headers are:

  • HTTP Strict Transport Security
  • X-Frame-Options
  • X-XSS-Protection
  • X-Content-Type-Options
  • Referrer-Policy
  • Feature-Policy
  • Content-Security-Policy
<IfModule mod_headers.c>
    Header set X-XSS-Protection "1; mode=block"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-Content-Type-Options "nosniff"
    Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
    Header set Referrer-Policy "same-origin"
    Header set Feature-Policy "geolocation 'self'; vibrate 'none'"
    Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
    Header always set Expect-CT "max-age=7776000, enforce"
    Header always set Referrer-Policy: "no-referrer-when-downgrade"
    Header always set Permissions-Policy "geolocation=(); midi=();notifications=();push=();sync-xhr=();accelerometer=(); gyroscope=(); magnetometer=(); payment=(); camera=(); microphone=();usb=(); xr=();speaker=(self);vibrate=();fullscreen=(self);"  
    Header always set Content-Security-Policy "upgrade-insecure-requests"
    Header set X-Permitted-Cross-Domain-Policies &quot;none&quot;

13. Disable directory browsing on your site

When the web server does not find an index file like index.php or index.html, it automatically displays a index page which shows the contents of the directory being vulnerable to hacker attacks.

Options -Indexes

14. File password protection

To increase the robustness of our fullfilment of security requirements in apache and wordpress we can include a password to access certain files and directories.

# proteger un archivo con contraseña
AuthType Basic
AuthName "Prompt"
AuthUserFile /home/path/.htpasswd
Require valid-user
# proteger un directorio con contraseña
AuthType basic
AuthName "This directory is protected"
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user

15. Protect the htaccess file

With these rules you can protect access to any file, showing a 403 error page when someone tries to access:

# Protege el archivo htaccess
<files .htaccess>
order allow,deny
deny from all

# Protección extrema de htaccess
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all

16 Block web access by IP

If you observe too much spam in the comments or notes an increase in traffic of dubious reputation you have the option to block this access to the web knowing the IP.

allow from all
deny from
deny from 124.150.011

17. Redirect all traffic except certain IPs

Sometimes we may be interested in closing the web to all traffic except for certain IP's showing a 403 (accessing a folder to which we do not have permission).

ErrorDocument 403
Order deny,allow
Deny from all
Allow from
Allow from

18. Block access to bots

With the following rule we can block all user-agents that we do not want our website to be tracked, either because it is potentially harmful or to keep the load on the server low. We must first identify those bots and their user agents. For example, if we do not want the Ahrefs bot to track us, we will use the user-agent “User-agent: AhrefsBot”.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} ^AhrefsBot[NC]
    RewriteRule ^.* - [F,L]

19. Prevent Hotlinking in htaccess

This rule helps us prevent other websites from using our images in their blogs hosted on our server thus reducing bandwidth consumption.

Options +FollowSymlinks
# Protect against hotlinking
Rewrit eEngine On
Rewrit eCond %{HTTP_REFERER} !^$
Rewrit eCond %{HTTP_REFERER} !^http://(www.)? [nc]
Rewrit eRule .*.(gif|jpg|png)$[nc]


Contact us to talk about your business. Our technicians and professionals will teach you the possibilities of improving your web positioning.


There are a large number of rules that can help us both in tasks SEO such as redirections or with friendly urls but also in web security. Although it is true that we cannot control 100% security, these simple rules will help us a lot in this regard.


Job Title
Htaccess SEO Wordpress
Article name
Htaccess SEO Wordpress
Know the best htaccess rules SEO Wordpress to work on your projects SEO. Improve the SEO and fix bugs in htaccess.
Posted by
Millennials Consulting


Beatriz Saavedra Santos
Beatriz Saavedra Santos
23. May, 2024.
Unbeatable team, absolute availability, service of 10. They have done the impossible in record time. Thanks for everything.
Walter Bulnes
Walter Bulnes
8. May, 2024.
It is a pleasure to greet you, we are a Chuck E. Cheese franchisee for Honduras, we had the opportunity to develop the Plan SEO with Millennials which has been very favorable, on our part we are very grateful and satisfied with the work done by the consultants who provided us with support (Álvaro Poveda/Enrique Monzó), very professional, very assertive, I can recommend them to potential clients who want this support.
Daniel Suarez
Daniel Suarez
17. April, 2024.
We have collaborated with Millennials Consulting for the implementation of CRM and other Zoho modules. The experience has been very positive, highlighting his professionalism and knowledge. The team of Millennials was well suited to our needs, ensuring a smooth and efficient integration. Additionally, they assisted us with the process of obtaining Digital Kit grants, which we greatly appreciated. I recommend to Millennials Consulting to those who need to implement Zoho solutions effectively.
Retirement Group
Retirement Group
17. April, 2024.
We have worked very well with them. Very good project organization and availability. Our consultant CRM José Iñesta has always guided and advised us very accurately. Totally recommended
Zine Hospitality
Zine Hospitality
22. March, 2024.
José showed a deep knowledge of the CRM and a great ability to adapt it to our specific needs. He effectively guided us through the implementation of bluePrints, workflows and fields in different modules, providing us with useful solutions and advice for our business. Thank you for your patience and dedication!


team photo millennials 2023
COMPANIES have trusted us and digitized their business in MC
PROJECTS have been contracted by companies in the last 2 years.
of companies REPEAT working with Millennials Consulting in new projects.
0 %





Raimundo Fernández Villaverde, 61, 5º Iz, 28003 Madrid


C. Mª Cristina 74, ground floor, 03660

Palma de Mallorca

Gran Via Asima 2 Floor 9 07009

Santa Cruz de Tenerife

Local Boston Street No. 2, 38650

Guadalajara - Mexico

Córdoba 2562, Providencia 3a. Sect, 44630.

Vitacura - Chile

Av Tabancura 1515 Office 309, 7630000 Vitacura.




We manage your business processes through software Zoho CRM for Business, which has an "all in 1" suite to manage your entire company, more than 40 tools interconnected with each other to better reach your business strategy.


It is not just being on the internet, your customers must find you through search engines. In Millennials Consulting We manage strategies positioning SEO so that your potential customers reach you first before your competition.

The website is the main letter of introduction of your business on the internet. "If you're not on the internet, you don't exist." A web design that communicates and reach your users will make the difference between your users and competitors by increasing your web traffic.

La digital strategies they are growing and we must be on the sidelines of this progress, that is why we connect tools that help to meet objectives.

We apply strategies Business Intelligence analyzing business data. We use the most sophisticated tools in the treatment and analysis of business data.

The connectivity of digital tools is one of our main pieces of digitization, using our alliances with partnerdigital s.