Skip to content
intermediate Phase 38 · Database Deep Dive

URL Rewrite System

URL rewrite system including url_rewrite table, product and category URLs, custom rewrites, and redirects

45m
0 problems
Topic Progress 0%

URL Rewrite Table Structure

url_rewrite Table

CREATE TABLE url_rewrite (
    redirect_id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Redirect ID',
    entity_type VARCHAR(32) NOT NULL COMMENT 'Entity Type',
    entity_id INT UNSIGNED NOT NULL COMMENT 'Entity ID',
    request_path VARCHAR(255) DEFAULT NULL COMMENT 'Request Path',
    target_path VARCHAR(255) DEFAULT NULL COMMENT 'Target Path',
    redirect_type SMALLINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Redirect Type',
    store_id SMALLINT UNSIGNED NOT NULL COMMENT 'Store ID',
    description VARCHAR(255) DEFAULT NULL COMMENT 'Description',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Created At',
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated At',
    is_autogenerated SMALLINT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Is Auto-generated',
    PRIMARY KEY (redirect_id),
    UNIQUE INDEX IDX_REQUEST_PATH_STORE_ID (request_path, store_id),
    INDEX IDX_ENTITY_ID (entity_id),
    INDEX IDX_STORE_ID (store_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='URL Rewrites';

Entity Types

Entity Type Description
product Product URL rewrite
category Category URL rewrite
cms-page CMS page URL rewrite
custom Custom URL rewrite

Key Columns

Column Description
request_path User-friendly URL (e.g., t-shirt.html)
target_path Internal target (e.g., catalog/product/view/id/123)
redirect_type 0=rewrite, 301=permanent, 302=temporary
is_autogenerated 0=custom, 1=system-generated
store_id Store view ID

Product URL Rewrites

Auto-Generated Product URLs

-- Product URL rewrites
SELECT request_path, target_path, redirect_type, is_autogenerated
FROM url_rewrite
WHERE entity_type = 'product' AND entity_id = 123 AND store_id = 1;
+---------------------------+-------------------------------------+---------------+------------------+
| request_path              | target_path                         | redirect_type | is_autogenerated |
+---------------------------+-------------------------------------+---------------+------------------+
| t-shirt-blue.html         | catalog/product/view/id/123         |             0 |                1 |
| electronics/phones/iphone.html | catalog/product/view/id/123    |             0 |                1 |
+---------------------------+-------------------------------------+---------------+------------------+

URL Key Attribute

// Product URL key attribute
$product->setUrlKey('my-product-url');
$product->save();

// Auto-generate from name
$product->setName('iPhone 15 Pro Max');
// Auto-generated url_key: 'iphone-15-pro-max'

// Get product URL
$url = $product->getUrlInStore(['_store' => $storeId]);
// Returns: https://example.com/iphone-15-pro-max.html

URL Suffix Configuration

<!-- etc/config.xml -->
<config>
    <default>
        <catalog>
            <seo>
                <product_url_suffix>.html</product_url_suffix>
                <category_url_suffix>.html</category_url_suffix>
            </seo>
        </catalog>
    </default>
</config>

Category URL Rewrites

Category URL Structure

-- Category URL rewrites
SELECT * FROM url_rewrite 
WHERE entity_type = 'category' AND entity_id = 5 AND store_id = 1;
+--------------+-------------+-----------+----------------------------+-------------------------------------+---------------+----------+
| redirect_id  | entity_type | entity_id | request_path               | target_path                         | redirect_type | store_id |
+--------------+-------------+-----------+----------------------------+-------------------------------------+---------------+----------+
|          789 | category    |         5 | electronics/phones.html    | catalog/category/view/id/5           |             0 |        1 |
+--------------+-------------+-----------+----------------------------+-------------------------------------+---------------+----------+

Category URL Path

-- Category URL path is stored in the entity table
SELECT entity_id, name, url_path, url_key 
FROM catalog_category_entity_varchar v
JOIN eav_attribute a ON a.attribute_id = v.attribute_id
WHERE a.attribute_code IN ('url_path', 'url_key')
AND v.entity_id = 5;
+-----------+--------+---------------------+--------+
| entity_id | name   | url_path            | url_key|
+-----------+--------+---------------------+--------+
|         5 | Phones | electronics/phones  | phones |
+-----------+--------+---------------------+--------+

Category URL with Path

// Get category URL
$category = $categoryRepository->get(5, $storeId);
$url = $category->getUrl();
// Returns: https://example.com/electronics/phones.html

// Category with custom URL key
$category->setUrlKey('smartphones');
$category->save();
// URL becomes: https://example.com/smartphones.html

Custom Rewrites and Redirects

Custom URL Rewrites

-- Add custom URL rewrite
INSERT INTO url_rewrite (entity_type, entity_id, request_path, target_path, redirect_type, store_id, is_autogenerated)
VALUES ('custom', 0, 'old-page', 'new-page', 0, 1, 0);

-- Redirect types:
-- 0 = No redirect (rewrite only)
-- 301 = Permanent redirect
-- 302 = Temporary redirect

Admin URL Rewrites

Admin > Marketing > URL Rewrites > Add URL Rewrite

1. Custom URL Rewrite:
   - Request Path: old-product-url
   - Target Path: new-product-url
   - Redirect Type: 301 Permanent

2. Product URL Rewrite:
   - Product: Select product
   - Custom URL: custom-slug

3. Category URL Rewrite:
   - Category: Select category
   - Custom URL: custom-category-slug

URL Rewrite Regeneration

# Regenerate all URL rewrites
bin/magento urlrewrite:reindex

# Regenerate for specific store
bin/magento urlrewrite:reindex --store=1

# Clear and regenerate
bin/magento urlrewrite:reindex --cleanup

URL Rewrite in Code

// Programmatic URL rewrite creation
$urlRewriteFactory = $objectManager->get(
    \Magento\UrlRewrite\Model\UrlRewriteFactory::class
);

$urlRewrite = $urlRewriteFactory->create();
$urlRewrite->setEntityType('custom')
    ->setEntityId(0)
    ->setRequestPath('old-path')
    ->setTargetPath('new-path')
    ->setRedirectType(301)
    ->setStoreId(1)
    ->setIsAutogenerated(0)
    ->save();

// Delete URL rewrite
$urlRewrite->delete();

Redirect Types

Type HTTP Code Use Case
0 None URL rewrite without redirect
301 Moved Permanently SEO-friendly permanent redirect
302 Found (Temporary) Temporary redirect for A/B testing

Quiz

1. What does the redirect_type column in url_rewrite store?

Question 1 options

2. What is the is_autogenerated flag used for?

Question 2 options

3. How do you regenerate all URL rewrites?

Question 3 options

Flashcards

Question

What is the url_rewrite table?

Answer

Stores URL rewrites mapping request_path to target_path with redirect types

Question

What are redirect types?

Answer

0 (rewrite only), 301 (permanent), 302 (temporary)

Question

What is is_autogenerated?

Answer

Flag marking system-generated rewrites vs custom rewrites

Question

How to regenerate URL rewrites?

Answer

bin/magento urlrewrite:reindex

Question

What entity types exist?

Answer

product, category, cms-page, custom

Revision Notes

Key Takeaways

  • 1. url_rewrite maps request_path to target_path with redirect types
  • 2. Product and category URLs are auto-generated from URL keys
  • 3. Custom rewrites can be created via admin or code
  • 4. Redirect types: 0 (none), 301 (permanent), 302 (temporary)
  • 5. Use urlrewrite:reindex to regenerate all URL rewrites

Interview Tips

  • Explain the difference between URL rewrite and redirect
  • Discuss SEO implications of 301 vs 302 redirects
  • Describe how URL rewrites are generated for products and categories

Cheat Sheet

URL Rewrite Table:
  redirect_id, entity_type, entity_id,
  request_path, target_path, redirect_type,
  store_id, is_autogenerated

Entity Types:
  product, category, cms-page, custom

Redirect Types:
  0   = Rewrite (no redirect)
  301 = Permanent redirect
  302 = Temporary redirect

Commands:
  bin/magento urlrewrite:reindex
  bin/magento urlrewrite:reindex --store=1
  bin/magento urlrewrite:reindex --cleanup