301 Redirects Setup During Site Migration

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1 servicesAll 2065 services
301 Redirects Setup During Site Migration
Medium
from 1 business day to 3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

301-Redirect Configuration During Site Migration

When moving a site to a new URL structure, changing domain or redesign — 301-redirects preserve accumulated SEO weight of pages. Without them, search engines perceive old URLs as deleted pages, and rankings fall.

What is 301 and Why It's Important

301 (Moved Permanently) transfers 90 to 99% of PageRank to the new page. Google usually transfers rankings within 2–8 weeks after correct redirect setup. 302 (Moved Temporarily) does not transfer weight, use only for temporary moves.

Migration Strategy

Before the move:

  1. Crawl old site (Screaming Frog, Sitebulb) — export all indexable URLs
  2. Check Google Search Console — find pages with traffic and rankings
  3. Match old URLs with new (mapping table)
  4. Prioritize high-traffic pages

Mapping table:

Old URL New URL Priority
/catalog.php?cat=12 /catalog/smartphones High
/product.php?id=4521 /products/iphone-15-pro High
/about.html /o-kompanii Medium
/news/2018/post-1 /blog/post-1-slug Low

Implementation in Laravel (DB Storage)

Schema::create('redirects', function (Blueprint $table) {
    $table->id();
    $table->string('from_url', 2048)->unique();
    $table->string('to_url', 2048);
    $table->smallInteger('status_code')->default(301);
    $table->boolean('is_regex')->default(false);
    $table->integer('sort_order')->default(0);
    $table->timestamps();
});
// app/Http/Middleware/HandleRedirects.php
class HandleRedirects
{
    public function handle(Request $request, Closure $next): Response
    {
        $path = '/' . ltrim($request->getPathInfo(), '/');

        // Exact match from Redis cache
        $redirect = Cache::remember("redirect:{$path}", 3600, function () use ($path) {
            return Redirect::where('from_url', $path)
                ->where('is_regex', false)
                ->first();
        });

        if ($redirect) {
            return redirect($redirect->to_url, $redirect->status_code);
        }

        // Regex redirects (less performant, no cache)
        $regexRedirects = Cache::remember('redirects:regex', 3600, function () {
            return Redirect::where('is_regex', true)
                ->orderBy('sort_order')
                ->get();
        });

        foreach ($regexRedirects as $redirect) {
            if (preg_match($redirect->from_url, $path, $matches)) {
                $to = preg_replace($redirect->from_url, $redirect->to_url, $path);
                return redirect($to, $redirect->status_code);
            }
        }

        return $next($request);
    }
}

Bulk Import from CSV

// Artisan command: import:redirects storage/redirects.csv
public function handle(): void
{
    $file = fopen($this->argument('file'), 'r');
    fgetcsv($file); // skip header

    $batch = [];
    while ($row = fgetcsv($file)) {
        $batch[] = [
            'from_url'    => trim($row[0]),
            'to_url'      => trim($row[1]),
            'status_code' => (int) ($row[2] ?? 301),
            'created_at'  => now(),
            'updated_at'  => now(),
        ];

        if (count($batch) >= 500) {
            Redirect::upsert($batch, ['from_url'], ['to_url', 'status_code']);
            $batch = [];
        }
    }
    if ($batch) {
        Redirect::upsert($batch, ['from_url'], ['to_url', 'status_code']);
    }

    Cache::flush(); // flush redirect cache
    $this->info('Import completed');
}

Nginx Configuration for Mass Redirects

For thousands of redirects — use Nginx map (more performant than middleware):

map $uri $redirect_to {
    include /etc/nginx/redirects.map;
}

server {
    if ($redirect_to) {
        return 301 $redirect_to;
    }
}
# redirects.map
/old-page           /new-page;
/catalog.php        /catalog;

Generate redirects.map script from database on deploy.

Domain Redirect

server {
    listen 80;
    server_name old-domain.ru www.old-domain.ru;
    return 301 https://new-domain.ru$request_uri;
}

Post-Migration Check

  • Screaming Frog: crawl new site, check redirect chains (no chains longer than 1 step)
  • Google Search Console: monitor Coverage → Not Found (404)
  • Ahrefs/Semrush: verify backlinks are correctly redirected
  • Check www. and non-www versions of domain

Setup timeline: 1–3 days depending on mapping volume and structure complexity.