Web development and examples of PHP, jQuery, HTML5, WordPress, MySQL & AMP


Redirect 301 in .htaccess with ‘redirect 301’ and ‘redirectmatch 301

Configure a redirect 301 in .htaccess is very easy. This redirection is used when we need to change urls of our websites and not lose the positioning of these urls when redirecting them.

This redirect warns search engines that the content has been permanently redirected to another url. Then the search engines reindex the new urls and we do not lose the positioning that we had gained before.

It is important that when I talk about url I mean that redirections can be applied to both server files and pages.

I will do the practical cases in .htaccess.

Among the examples we will see practical cases such as the redirection of a URL, domain redirection and directory redirection. The functions used will be:

  • .htaccess redirect 301
  • .htaccess redirectmatch 301
  • .htaccess redirectmatch permanent
  • .htaccess RewriteRule

Difference between ‘redirect 301’ and ‘redirectmatch 301

redirect 301‘ is used to redirect a single url for each line of code in the .htaccess

While ‘redirectmatch 301‘ is used when we want to establish redirection rules using regular expressions (patterns), and thus all urls that meet those rules will be redirected to the new ones.

Redirecting a URL with ‘redirect 301

In this example code we want to redirect a url to another url.

Redirect 301 urls based on regular expressions with ‘redirectmatch 301

The use of regular expressions in .htaccess is widely used due to the code saving it implies:

Redirect 301 with ‘RewriteRule

RewriteRule does the same function as RedirectMatch only with different declaration, I give you an example:

With these 3 ways of doing redirect 301 we cover most of the needs that we will usually have.

Cookie Policy

This website uses cookies to provide you with the best user experience. If you continue to browsing you are consenting to the acceptance of these cookies and acceptance of our cookie policy, click the link for more information.

ACCEPT
Aviso de cookies