Skip to main content

Get the expected Magento2 Product URL Key value

Magento2 Product URL Key (URL key)


The URL key is the part of a static URL that describes the product or category. When you create a product or category, an initial URL key is automatically generated, based on the name. To change the URL key, see the Search Engine Optimization section of the product information.

The URL key should consist of lowercase characters with hyphens to separate words. A well-designed, “search engine friendly” URL key might include the product name and key words to improve the way it is indexed by search engines. The URL key can be configured to create an automatic redirect if the URL key changes.

Get the expected product URL Key (url_key) value in Magento2

Sometime you want to know the expected URL Key for an product to see if it will be okay or  not, the best way I have seen is to see how Magento itself create it, for that the easy way is to create a new product with any name and get the product id , then load the product ... see below 

 

$_productloader = $objectManager->get('\Magento\Catalog\Model\ProductFactory');
$product =  $_productloader->create()->load(1);


echo  $product->formatUrlKey("dsdasdad ff +_432765&*&^(68789(??T");

 

in this case you will have the expected URL Key

have fun :)

We have created a independent class that can do this job

 

 

<?php
class UrlKey
{
    protected $convertTable = [
        '&amp;' => 'and',
        '@' => 'at',
        '©' => 'c',
        '®' => 'r',
        'À' => 'a',
        'Á' => 'a',
        'Â' => 'a',
        'Ä' => 'a',
        'Å' => 'a',
        'Æ' => 'ae',
        'Ç' => 'c',
        'È' => 'e',
        'É' => 'e',
        'Ë' => 'e',
        'Ì' => 'i',
        'Í' => 'i',
        'Î' => 'i',
        'Ï' => 'i',
        'Ò' => 'o',
        'Ó' => 'o',
        'Ô' => 'o',
        'Õ' => 'o',
        'Ö' => 'o',
        'Ø' => 'o',
        'Ù' => 'u',
        'Ú' => 'u',
        'Û' => 'u',
        'Ü' => 'u',
        'Ý' => 'y',
        'ß' => 'ss',
        'à' => 'a',
        'á' => 'a',
        'â' => 'a',
        'ä' => 'a',
        'å' => 'a',
        'æ' => 'ae',
        'ç' => 'c',
        'è' => 'e',
        'é' => 'e',
        'ê' => 'e',
        'ë' => 'e',
        'ì' => 'i',
        'í' => 'i',
        'î' => 'i',
        'ï' => 'i',
        'ò' => 'o',
        'ó' => 'o',
        'ô' => 'o',
        'õ' => 'o',
        'ö' => 'o',
        'ø' => 'o',
        'ù' => 'u',
        'ú' => 'u',
        'û' => 'u',
        'ü' => 'u',
        'ý' => 'y',
        'þ' => 'p',
        'ÿ' => 'y',
        'Ā' => 'a',
        'ā' => 'a',
        'Ă' => 'a',
        'ă' => 'a',
        'Ą' => 'a',
        'ą' => 'a',
        'Ć' => 'c',
        'ć' => 'c',
        'Ĉ' => 'c',
        'ĉ' => 'c',
        'Ċ' => 'c',
        'ċ' => 'c',
        'Č' => 'c',
        'č' => 'c',
        'Ď' => 'd',
        'ď' => 'd',
        'Đ' => 'd',
        'đ' => 'd',
        'Ē' => 'e',
        'ē' => 'e',
        'Ĕ' => 'e',
        'ĕ' => 'e',
        'Ė' => 'e',
        'ė' => 'e',
        'Ę' => 'e',
        'ę' => 'e',
        'Ě' => 'e',
        'ě' => 'e',
        'Ĝ' => 'g',
        'ĝ' => 'g',
        'Ğ' => 'g',
        'ğ' => 'g',
        'Ġ' => 'g',
        'ġ' => 'g',
        'Ģ' => 'g',
        'ģ' => 'g',
        'Ĥ' => 'h',
        'ĥ' => 'h',
        'Ħ' => 'h',
        'ħ' => 'h',
        'Ĩ' => 'i',
        'ĩ' => 'i',
        'Ī' => 'i',
        'ī' => 'i',
        'Ĭ' => 'i',
        'ĭ' => 'i',
        'Į' => 'i',
        'į' => 'i',
        'İ' => 'i',
        'ı' => 'i',
        'IJ' => 'ij',
        'ij' => 'ij',
        'Ĵ' => 'j',
        'ĵ' => 'j',
        'Ķ' => 'k',
        'ķ' => 'k',
        'ĸ' => 'k',
        'Ĺ' => 'l',
        'ĺ' => 'l',
        'Ļ' => 'l',
        'ļ' => 'l',
        'Ľ' => 'l',
        'ľ' => 'l',
        'Ŀ' => 'l',
        'ŀ' => 'l',
        'Ł' => 'l',
        'ł' => 'l',
        'Ń' => 'n',
        'ń' => 'n',
        'Ņ' => 'n',
        'ņ' => 'n',
        'Ň' => 'n',
        'ň' => 'n',
        'ʼn' => 'n',
        'Ŋ' => 'n',
        'ŋ' => 'n',
        'Ō' => 'o',
        'ō' => 'o',
        'Ŏ' => 'o',
        'ŏ' => 'o',
        'Ő' => 'o',
        'ő' => 'o',
        'Œ' => 'oe',
        'œ' => 'oe',
        'Ŕ' => 'r',
        'ŕ' => 'r',
        'Ŗ' => 'r',
        'ŗ' => 'r',
        'Ř' => 'r',
        'ř' => 'r',
        'Ś' => 's',
        'ś' => 's',
        'Ŝ' => 's',
        'ŝ' => 's',
        'Ş' => 's',
        'ş' => 's',
        'Š' => 's',
        'š' => 's',
        'Ţ' => 't',
        'ţ' => 't',
        'Ť' => 't',
        'ť' => 't',
        'Ŧ' => 't',
        'ŧ' => 't',
        'Ũ' => 'u',
        'ũ' => 'u',
        'Ū' => 'u',
        'ū' => 'u',
        'Ŭ' => 'u',
        'ŭ' => 'u',
        'Ů' => 'u',
        'ů' => 'u',
        'Ű' => 'u',
        'ű' => 'u',
        'Ų' => 'u',
        'ų' => 'u',
        'Ŵ' => 'w',
        'ŵ' => 'w',
        'Ŷ' => 'y',
        'ŷ' => 'y',
        'Ÿ' => 'y',
        'Ź' => 'z',
        'ź' => 'z',
        'Ż' => 'z',
        'ż' => 'z',
        'Ž' => 'z',
        'ž' => 'z',
        'ſ' => 'z',
        'Ə' => 'e',
        'ƒ' => 'f',
        'Ơ' => 'o',
        'ơ' => 'o',
        'Ư' => 'u',
        'ư' => 'u',
        'Ǎ' => 'a',
        'ǎ' => 'a',
        'Ǐ' => 'i',
        'ǐ' => 'i',
        'Ǒ' => 'o',
        'ǒ' => 'o',
        'Ǔ' => 'u',
        'ǔ' => 'u',
        'Ǖ' => 'u',
        'ǖ' => 'u',
        'Ǘ' => 'u',
        'ǘ' => 'u',
        'Ǚ' => 'u',
        'ǚ' => 'u',
        'Ǜ' => 'u',
        'ǜ' => 'u',
        'Ǻ' => 'a',
        'ǻ' => 'a',
        'Ǽ' => 'ae',
        'ǽ' => 'ae',
        'Ǿ' => 'o',
        'ǿ' => 'o',
        'ə' => 'e',
        'Ё' => 'jo',
        'Є' => 'e',
        'І' => 'i',
        'Ї' => 'i',
        'А' => 'a',
        'Б' => 'b',
        'В' => 'v',
        'Г' => 'g',
        'Д' => 'd',
        'Е' => 'e',
        'Ж' => 'zh',
        'З' => 'z',
        'И' => 'i',
        'Й' => 'j',
        'К' => 'k',
        'Л' => 'l',
        'М' => 'm',
        'Н' => 'n',
        'О' => 'o',
        'П' => 'p',
        'Р' => 'r',
        'С' => 's',
        'Т' => 't',
        'У' => 'u',
        'Ф' => 'f',
        'Х' => 'h',
        'Ц' => 'c',
        'Ч' => 'ch',
        'Ш' => 'sh',
        'Щ' => 'sch',
        'Ъ' => '-',
        'Ы' => 'y',
        'Ь' => '-',
        'Э' => 'je',
        'Ю' => 'ju',
        'Я' => 'ja',
        'а' => 'a',
        'б' => 'b',
        'в' => 'v',
        'г' => 'g',
        'д' => 'd',
        'е' => 'e',
        'ж' => 'zh',
        'з' => 'z',
        'и' => 'i',
        'й' => 'j',
        'к' => 'k',
        'л' => 'l',
        'м' => 'm',
        'н' => 'n',
        'о' => 'o',
        'п' => 'p',
        'р' => 'r',
        'с' => 's',
        'т' => 't',
        'у' => 'u',
        'ф' => 'f',
        'х' => 'h',
        'ц' => 'c',
        'ч' => 'ch',
        'ш' => 'sh',
        'щ' => 'sch',
        'ъ' => '-',
        'ы' => 'y',
        'ь' => '-',
        'э' => 'je',
        'ю' => 'ju',
        'я' => 'ja',
        'ё' => 'jo',
        'є' => 'e',
        'і' => 'i',
        'ї' => 'i',
        'Ґ' => 'g',
        'ґ' => 'g',
        'א' => 'a',
        'ב' => 'b',
        'ג' => 'g',
        'ד' => 'd',
        'ה' => 'h',
        'ו' => 'v',
        'ז' => 'z',
        'ח' => 'h',
        'ט' => 't',
        'י' => 'i',
        'ך' => 'k',
        'כ' => 'k',
        'ל' => 'l',
        'ם' => 'm',
        'מ' => 'm',
        'ן' => 'n',
        'נ' => 'n',
        'ס' => 's',
        'ע' => 'e',
        'ף' => 'p',
        'פ' => 'p',
        'ץ' => 'C',
        'צ' => 'c',
        'ק' => 'q',
        'ר' => 'r',
        'ש' => 'w',
        'ת' => 't',
        '™' => 'tm',
        'α' => 'a',
        'ά' => 'a',
        'Ά' => 'a',
        'Α' => 'a',
        'β' => 'b',
        'Β' => 'b',
        'γ' => 'g',
        'Γ' => 'g',
        'δ' => 'd',
        'Δ' => 'd',
        'ε' => 'e',
        'έ' => 'e',
        'Ε' => 'e',
        'Έ' => 'e',
        'ζ' => 'z',
        'Ζ' => 'z',
        'η' => 'i',
        'ή' => 'i',
        'Η' => 'i',
        'θ' => 'th',
        'Θ' => 'th',
        'ι' => 'i',
        'ί' => 'i',
        'ϊ' => 'i',
        'ΐ' => 'i',
        'Ι' => 'i',
        'Ί' => 'i',
        'κ' => 'k',
        'Κ' => 'k',
        'λ' => 'l',
        'Λ' => 'l',
        'μ' => 'm',
        'Μ' => 'm',
        'ν' => 'n',
        'Ν' => 'n',
        'ξ' => 'x',
        'Ξ' => 'x',
        'ο' => 'o',
        'ό' => 'o',
        'Ο' => 'o',
        'Ό' => 'o',
        'π' => 'p',
        'Π' => 'p',
        'ρ' => 'r',
        'Ρ' => 'r',
        'σ' => 's',
        'ς' => 's',
        'Σ' => 's',
        'τ' => 't',
        'Τ' => 't',
        'υ' => 'u',
        'ύ' => 'u',
        'Υ' => 'y',
        'Ύ' => 'y',
        'φ' => 'f',
        'Φ' => 'f',
        'χ' => 'ch',
        'Χ' => 'ch',
        'ψ' => 'ps',
        'Ψ' => 'ps',
        'ω' => 'o',
        'ώ' => 'o',
        'Ω' => 'o',
        'Ώ' => 'o',
        'অ' => 'a',
        'আ' => 'aa',
        'ই' => 'i',
        'ঈ' => 'ii',
        'উ' => 'u',
        'ঊ' => 'uu',
        'ঋ' => 'r',
        'ৠ' => 'ri',
        'এ' => 'e',
        'ঐ' => 'ai',
        'ও' => 'o',
        'ঔ' => 'ou',
        'ক' => 'ka',
        'খ' => 'kha',
        'গ' => 'ga',
        'ঘ' => 'gha',
        'ঙ' => 'na',
        'চ' => 'ca',
        'ছ' => 'cha',
        'জ' => 'ja',
        'ঝ' => 'jha',
        'ঞ' => 'na',
        'ট' => 'ta',
        'ঠ' => 'tha',
        'ড' => 'da',
        'ড়' => 'ra',
        'ঢ' => 'dha',
        'ঢ়' => 'rha',
        'ণ' => 'na',
        'ত' => 'ta',
        'ৎ' => 't',
        'থ' => 'tha',
        'দ' => 'da',
        'ধ' => 'dha',
        'ন' => 'na',
        'প' => 'pa',
        'ফ' => 'pha',
        'ব' => 'ba',
        'ভ' => 'bha',
        'ম' => 'ma',
        'য' => 'ya',
        'য়' => 'ya',
        'র' => 'ra',
        'ল' => 'la',
        'শ' => 'sa',
        'ষ' => 'sha',
        'স' => 'sa',
        'হ' => 'ha',
        '০' => '0',
        '১' => '1',
        '২' => '2',
        '৩' => '3',
        '৪' => '4',
        '৫' => '5',
        '৬' => '6',
        '৭' => '7',
        '৮' => '8',
        '৯' => '9',
        'ক্ষ' => 'kso',
        'ষ্ণ' => 'sno',
        'জ্ঞ' => 'jno',
        'ঞ্জ' => 'nchho',
        'হ্ম' => 'hmo',
        'ঞ্চ' => 'ncho',
        'ঙ্ক' => 'ngko',
        'ট্ট' => 'tto',
        'ক্ষ্ম' => 'ksmo',
        'হ্ন' => 'hno',
        'হ্ণ' => 'hno',
        'ক্র' => 'kro',
        'গ্ধ' => 'gdho',
        'ত্র' => 'tro',
        'ক্ত' => 'kto',
        'ক্স' => 'kso',
        'ত্ত' => 'tto',
        'ত্ম' => 'tmo',
        'ক্ক' => 'kko',
        'ক্ম' => 'kmo',
        'ক্ল' => 'klo',
        'া' => 'a',
        'ি' => 'i',
        'ী' => 'ee',
        'ু' => 'o',
        'ূ' => 'u',
        'ৃ' => 'ri',
        'ৄ' => 'rii',
        'ে' => 'a',
        'ৈ' => 'ai',
        'ো' => 'o',
        'ৌ' => 'ow',
        '্য' => 'a',
        '্র' => 'r',
        'ঁ' => 'n',
        'ঃ' => 'oh',
        '়' => 'o',
        '্' => 'h',
        'ং' => 'ng',
        'ৢ' => 'n',
        'ৣ' => 'nn'
    ];
    const ICONV_CHARSET = "UTF-8";

    function getConvertTable()
    {
        return $this->convertTable;
    }

    private  function filter($string)
    {
        $string = strtr($string, $this->getConvertTable());
        return '"libiconv"' == ICONV_IMPL ? iconv(
            self::ICONV_CHARSET,
            'ascii//ignore//translit',
            $string
        ) : $string;
    }

    public function getURL($string)
    {
        {
            $string = preg_replace('#[^0-9a-z]+#i', '-', $this->filter($string));
            $string = strtolower($string);
            $string = trim($string, '-');

            return $string;
        }
    }
}
$url = new UrlKey();
echo  $url->getURL("dsdasd%$#7676?.>");

 

 

More info about the URL KEY click here