How to make 301 redirect in .htaccess file - detailed examples

print · Время на чтение: 13мин · - · Опубликовано · Обновлено

playListen to this article

301 redirect how to do - examples.

Let's take a look at the basic redirect rules that apply to the .htaccess file. Examples of how to make 301 redirects in .htaccess file. Why do you need a redirect and its features. The effect of redirects on the final page and how it will be useful.

301 redirect (Permanent Redirect) - this is one of the basic rules that must be written in .htaccess. The main task of 301 is Redirecta, redirecting the user to the page address specified in the rule.

The content of the article:

What is 301 Permanent Redirect and HTTP 301 status code?

301 redirect - what is it and how to set it up.

Permanent Redirect 301 is a standard indication that a page or resource has moved to a new location. Thus, the user, when visiting one page, is redirected to the address of its new location.

The search bot or other third-party bots receive the appropriate HTTP 301 response code. This means that the page or full-fledged resource has been permanently moved to a new address. Accordingly, all the main properties of the page go to the redirect endpoint. Indicating to search engines to review all current incoming URLs. For the subsequent transfer of weight to the final page (resource).

301 permanent redirect: This is the main term in SEO that is used when standardizing URLs. Maybe everyone knows this term, but since it is not used very often, many professional SEOs are not very familiar with permanent 301 redirects.

What is the effect of using 301 redirects:

A 301 permanent redirect is a redirect method that complies with search engine rules among all redirect methods. Other redirect methods include temporary 302 redirect, JS redirect, etc. These methods can result in bad URLs.

So if you have multiple domain names and want to allow them to visit the same website, you can only use one of them as your primary domain name and the remaining other domain names perform a permanent 301 redirect to that primary domain name . If different domain names visit the same site and the correct redirect is not re-enabled, this will penalize the site.

301 Permanent Redirect in Search Engine Optimization and Seo

All search engines, especially the main ones Google and Yandex, fully support the use of redirects. This allows you to indicate to the search robot that several different addresses need to be combined into 1. In this case, only the one to which the redirect is performed will be the main address.

For Seo, this is an indispensable additional promotion tool. For example, allowing you to specify the search engine:

  • To a new website address or a new domain. For the subsequent transfer of all properties of the old address (domain) to a new domain.
  • Redirect with add. mirrors on the main. For example, when a resource is available at www and without www. Or uses (https), but is also available via http.

Basic rules for using a redirect

  • First, you need to place the redirects in the correct order in the rule. Use the most frequent redirects (above) and the most global ones below.
  • Second, avoid multiple redirects. This will have an extremely negative impact on SEO website promotion. The search bot, or the user in this case, will be redirected multiple times. For example: from one page, to the 2nd, then to the 3rd. This is bad and incorrect from the point of view of SEO.
  • Thirdly, try not to do cyclic redirects (to each other, in a circle). This will negatively affect the distribution of weight between the pages specified in the rule.
  • Fourth - do not forget that most browsers use hashing. Therefore, all redacts must be checked for relevance by third-party resources.

How to set up 301 redirects in .htaccess file

The file itself .htaccess is located in the main directory of the site. Work with it is carried out very carefully, with any change, save a backup copy. One mistake in writing a rule will lead to an unexpected error on the site. Then it will be difficult to establish the cause of the banal error 500 (Internal Server Error). The editing itself is carried out using any text editor, necessarily with support for UTF-8 encoding (Unicode conversion format).

The main function, which indicates to the server that there are redirect rules, is set first.
All subsequent rules are written after it:

Options +FollowSymLinks RewriteEngine On

Redirect 301 directly related to the domain name and the site as a whole
These redirects include popular redirects:

  1. From WWW and back - to specify a mirror;
  2. Domain per domain or multiple domains per main;
  3. From HTTPS to HTTP and vice versa, to specify the preferred version;
  4. Directly complete transfer of the project to a new domain;

These are the most requested global redirects that are related to the domain name and website. Thanks to such rules, all transitions and designations pass smoothly and painlessly. One of the most sought after for search promotion. A common redirect from a www domain to a non-www one. It is necessary to indicate the main mirror to search engines.

RewriteCond %{HTTP_HOST} ^www.site\.ru$ [NC] RewriteRule ^(.*)$ http://site.ru/$1 [R=301,L]

Reverse redirect example:

RewriteCond %{HTTP_HOST} ^site\.ru$ [NC] RewriteRule ^(.*)$ http://www.site.ru/$1 [R=301,L]

For those using HTTPS:

RewriteCond %{SERVER_PORT} ^80$ [OR] RewriteCond %{HTTP} =on RewriteRule ^(.*)$ https://pixelplus.ru/$1 [R=301,L]

301 - a redirect from a domain to a domain or to a domain in the RF zone.

Redirecting from one domain name to another.

RewriteCond %{HTTP_HOST} ^old-site\.ru$ [NC] RewriteRule ^(.*)$ http://www.site.ru/$1 [R=301,L]

For owners of a domain name in the Russian Federation zone:

RewriteCond %{HTTP_HOST} ^xn-...\.xn--p1ai$ [NC] RewriteRule ^(.*)$ http://www.site.ru/$1 [R=301,L]

Switching the site to HTTPS for all pages of the site

Thus, all existing project addresses will be directed to new ones with the “https” prefix.

RewriteCond %{SERVER_PORT} ^80$ [OR] RewriteCond %{HTTP} =on RewriteRule ^(.*)$ https://domain.ru/$1 [R=301,L]

Back to HTTP

It is possible if you need to switch to http, although it is rarely used.

RewriteCond %{HTTPS} "on" RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]

Domain redirect to www address with subdirectory

RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} domain.com [NC] RewriteRule ^(.*)$ http://www.domain.com/directory/index.html [R=301,NC]

Redirect from the old domain to the new domain, which includes the full path and query string

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

Redirect from old domain with subdirectory to new domain without subdirectory, but with full path and query string

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$ RewriteRule ^(.*) http://www.katcode.com/%1 [R=302,NC]

Full site relocation to a new domain

A cool thing that is essential for moving the site to a new domain.

RewriteCond %{REQUEST_FILENAME} robots.txt$ [NC] RewriteRule ^([^/]+) $1 [L] RewriteCond %{HTTP_HOST} ^site\.ru RewriteRule ^(.*)$ http://new-site.ru /$1 [R=301,L] RewriteCond %{HTTP_HOST} ^www.site\.ru RewriteRule ^(.*)$ http://new-site.ru/$1 [R=301,L]

Redirect from the old domain to the new domain, which includes the full path and query string

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

Redirect from old domain with subdirectory to new domain without subdirectory, but with full path and query string

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_URI} ^/subdirname/(.*)$ RewriteRule ^(.*) http://www.katcode.com/%1 [R=302,NC]

Redirecting an entire site or domain to a new one

Redirect 301 / http://www.domain.com/

Redirect the entire site to a subfolder

Redirect 301 / http://www.domain.com/subfolder/

Redirect subfolder to another website

Redirect 301 /subfolder http://www.domain.com/

Redirect file extension but keep page name

RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php

301 redirect of site pages

Sometimes it is extremely necessary to carry out such redirection. There can be many reasons for this, especially for 404 error pages. In most cases, other domains lead to such pages. As we all understand, they convey the weight (TIC, PR, X, TRUST) that we need.

Permanent Redirect - allows you to direct a non-existent page to the required one with similar material. Thanks to this, we will save the passed properties, and the ps robots will update the data on incoming links. The main focus of these rules is:

  1. Specifying the preferred canonical page;
  2. Page direction from / (teared) to without it;
  3. Instructing search bots to the 404 page;
  4. Preferred section or directory;
  5. The main redirect is 301 from one page (old version) to the new one. Directs the robot or user to a new version of the page. Thus, we specify the canonical version of the page. It is possible to specify the desired redirect.
Redirect 301 /old-post.html http://new-site.ru/new-post.html

Forwarding addresses without / at the end to addresses with / at the end

Helps to remove unwanted page duplicates.

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

Reverse redirect of addresses with / at the end, to without it

It also helps to avoid duplicates.

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

Redirecting all pages of one domain to the main page of another

Helps to create a structure of multiple domains (sites).

RewriteCond %{REQUEST_URI} (.*) RewriteRule ^(.*)$ http://site.ru/ [L,R=301]

We specify through .htaccess the path to the 404 error file

An essential thing for competent search promotion.

ErrorDocument 404 /404-for-me.php

Redefining the site directory or category

This rule helps to avoid wasted time specifying redirects for individual addresses.

Option 1. RewriteRule ^(.*)/old-category/(.*)$ $1/new-category/$2 [R=301,L] Option 2. RewriteRule old-category /(.*) / old-category / $1 [R=301,L]

Redirect from blog subdomain to blog folder

Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_URI}/ blog RewriteRule ^(.*) http://www.nicola.top/%{REQUEST_URI} [R=302,NC] RewriteRule ^(.*) http://www .nicola.top/blog/%{REQUEST_URI} [R=302,NC]

Redirect one directory to another

Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*)/old-directory/(.*)$ $1/new-directory/$2 [R,L]

Single folder redirection

When redirecting one folder to a new location, you should use the following:

RewriteRule ^/?folder/(.\*)$ /location/$1 [R,L]

I use "folder" and "location" as examples. Replace folder and location names!

Delete index.html or index.php and redirect to root

RewriteEngine On RewriteCond %{THE_REQUEST} /index.php HTTP [NC] RewriteRule (.*)index.php$ /$1 [R=301,L]

 

RewriteEngine On RewriteCond %{THE_REQUEST} /index.html HTTP [NC] RewriteRule (.*)index.html$ /$1 [R=301,L]

301 redirect for url with GET parameters

A very useful rule that allows you to get rid of unwanted duplicates and other website snot. In particular, it will help owners of various CMS, especially WordPress.

URL example: https://nicola.top/page.php?id=15477852.

RewriteCond %{QUERY_STRING} id=15477852 RewriteRule .* http://site.ru/page.php? [R=301,L]

If necessary, remove all get parameters after the sign (?). You can use the following example:

RewriteRule(.*) $1? [R=301,L]

Position after: RewriteBase /

301 redirects for urls with extensions

Very good, especially with some errors in the url.

Option 1. RewriteRule ^(([^/]+/)*[^.]+)$ /$1.html [L] Option 2. RedirectMatch 301 (.*)\.php$ http://www.site.com$1 .html

Using PHP to set up a 301 redirect

This method will help those who do not use the Apache server. Rule analogue for php:

header("HTTP/1.1 301 Moved Permanently"); header("Location: http://corresponding site/dir/"); exit(); ?>

How to make a 301 redirect for sites on nginx

And the name is clear that this is a permanent redirect for those who have a site on nginx.

if ($host = 'www.domain.com' ) { rewrite ^(.*)$ http://domain.com$1 permanent; }

Basic regex syntax for Apache/.htaccess file

This regular expression can use any printable characters and space. However, some symbols have a special purpose:

() - are used to highlight the desired groups of characters.
^ is a special character at the beginning of a line.
$ - end-of-line special character.
. stands for any character.
| means an alternative. For example, the expressions “A|B” and “(ABC|DEF)” mean “A or B” and “ABC or DEF”, respectively.
? - is placed after a character (or group of characters), which may or may not be present.
* - put after a character (or group of characters), which may be absent or present an unlimited number of times in a row.
+ - acts similarly to the * character, with the only difference that the character preceding it must be present at least once.
[] - used to enumerate valid characters. Ranges can be used inside brackets: for example, the expression “[0-9]” is equivalent to the expression “[0123456789]”. If the characters inside the square brackets begin with a ^, it means any character other than those listed in the brackets.
\ - is placed in front of special characters if they are needed in their original form.
{3,9} - curly braces mean the range of the number of characters, in this case from 3 to 9 characters are allowed, the length of the string of characters must be equal to or greater than 3 equal to or less than 9 characters.
Anything after the '#' character is considered a comment.

Redirection plugin for site owners on CMS WorPress

Redirection - Plugin for WordPress.

A great solution for detecting 404 errors. One of the easiest ways to redirect a url. The Redirection plugin is the most popular redirect manager for WordPress. With it, you can easily manage url redirects, track 404 errors. Also, this plugin allows you to remove / at the end of url. This will help reduce the number of errors and improve the rating of your project.

redirection - allows you to create and manage redirects
Creates a redirect quickly and easily, no knowledge of Apache or Nginx required. If your WordPress site supports permalinks!!!! Then you can confidently use this plugin to redirect any URL on your site. The plugin has full support for regular expressions, so you can create Permanent Redirect templates with any number of URLs.

You can also set up a plugin to control your content. When a post or page changes, the plugin will automatically create the appropriate redirect to the new URL. In addition to the usual URL redirect. You have the option to redirect the URL based on other conditions:

  • The login status is redirect only if the user is logged in or logged out.
  • Browser - redirect if the user is using a specific browser.
  • Referrer - redirect if the user visited the link from another page.
  • Cookies - redirect if certain cookies are set.
  • HTTP headers - redirect, based on the HTTP header.
  • Custom filter - redirect, based on WordPress custom filter.

It also allows you to keep track of all 404 errors that occur on your site.

Redirection has APACHE & AMP & NGINX support

By default, this plugin will manage all redirects using WordPress. However, it can be configured so that all redirections are automatically stored in an .htaccess file and handled by Apache itself. If you are using Nginx, you can export the settings to an Nginx rewrite rules file. The plugin also has a fully featured import and export system:

  1. Import and export to Apache and .htaccess;
  2. Export to Nginx rewrite rules;
  3. Copy redirect between sites using JSON;
  4. Import and export to CSV for tabular viewing;
  5. Use WP CLI for automation.

Simple 301 Redirects plugin for WordPress

Simple 301 Redirects by BetterLinks – WordPress plugin.

Simple Redirects is an easy way to redirect requests to another page on your site or elsewhere on the web. Especially handy if you're migrating a WordPress site and can't keep a clean URL structure. By setting redirects from old pages to new ones, any incoming links will pass their stat weight.

SEO Redirection Plugin for WordPress

SEO Plugin - 301 RedirectManager.

SEO Redirection is a WordPress redirect plugin. You can easily create and manage redirects of your site. This plugin is extremely helpful! Especially if you need to migrate your URL structure from an old website or change your WordPress site directory.

What functionality does the SEO Redirection plugin have:

  1. Manually adding 301, 302 and 307 redirects in WordPress.
  2. Supports Redirect wild card.
  3. Fix 404 crawl errors in Google Search Console.
  4. Support for WPML and import/export features.
  5. Auto-adding a redirect when the URL changes.
  6. Direct redirects (pages, publications, drafts) after their changes.
  7. Extended control panel.
  8. You can redirect folders and all their contents.
  9. A complete log is kept for all redirected URLs.
  10. It is possible to use regular expressions in the redirect.
  11. Apache .htaccess is not required, the plugin only works in WordPress.
  12. Redirect index.php, index.html and index.htm.

Finally

Redirects are an essential thing in search engine promotion. Thanks to Permanent Redirect, you can get rid of duplicates, indicate the main source. Quite often, Rt is used to redirect 404 errors to other pages in the project.

Precautions for 301 redirects:

  1. In the 301 redirect process, we need to make sure that the content of the old and new URLs are highly relevant. If the two contents are not relevant, the redirect effect may be affected.
  2. If you're trying to use multiple content-related domain names, 301 redirects to a target domain name to boost your website's ranking, this is a relatively dangerous operation and can easily be labeled as a scam.

In this guide, I tried to paint all possible redirects in great detail. Making the most of the .htaccess file and available WordPress plugins. I also suggest you read a helpful post on how to create a robots.txt file for a website. I wish you success and good luck with your redirects.

Reading this article:

Thanks for reading: SEO HELPER | NICOLA.TOP

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 247

No votes so far! Be the first to rate this post.

Читайте также:

7 Responses

  1. Алия says:

    Hello, My site is not redirecting when I enter an IP address. What should I do to make it redirect from an IP address.

    • Николай Алексеев says:

      Make sure you are using a dedicated IP address that resolves to the server you are setting up the redirect on. Also, check if there is another .htaccess file that it might inherit the rule from, or if another rule in the same .htaccess file might be interfering with this redirect.

  2. Артур says:

    Hey, let me ask, before I break down my website optimization strategy, I need to concatenate two variables "www to non-www" and "remove the .php file extension before the slash". I'm using WordPress and I need to do a full 301 redirect to apply to all content: domain.com/category/post-name.html to domain.com/post-name/

    • Николай Алексеев says:

      Because WordPress uses .htaccess rules, I don't recommend adding rules to the file manually. Instead, I recommend setting up permalinks in your dashboard, and then using a 301 redirect plugin to permanently redirect any old URLs to the new ones. This will automatically add the rules to your .htaccess file according to the WordPress rules.

  3. Роман says:

    Hello dear,
    i pasted this but my site can't redirect,
    please, help

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^dubaivacancy.me [NC]
    RewriteRule ^(.*)$ domein.com/$1 [L,R=301,NC]

    Rewrite Engine On
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https:/%{HTTP_HOST}/$1 [R=301,L]

    # START WordPress

    Rewrite Engine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    • Николай Алексеев says:

      Thank you for your question. To clarify, are you getting any error message when you try to set up a redirect or is it just a redirect to the wrong place? You also need to make sure the .htaccess file has the correct name and is accessible from the website's document root (usually the public_html folder).

  4. Mason says:

    This information is worth everyone's attention. When can I find out more?

Добавить комментарий

Your email address will not be published. Обязательные поля помечены *

20 − 4 =