# UrlNormalizer

> UrlNormalizer normalizes URLs for yii\web\UrlManager and yii\web\UrlRule.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › UrlNormalizer

| class | `yii\web\UrlNormalizer` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0.10 |
| Source | [`framework/web/UrlNormalizer.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/UrlNormalizer.php#L22) |

UrlNormalizer normalizes URLs for [`UrlManager`](/docs/api/yii/web/urlmanager/index.md) and [`UrlRule`](/docs/api/yii/web/urlrule/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$action`](#property-action) | `int\|callable\|null` | Action to perform during route normalization. Default: `self::ACTION_REDIRECT_PERMANENT`. | `UrlNormalizer` |
| <span id="property-collapseSlashes"></span>`$collapseSlashes` | `bool` | Whether slashes should be collapsed, for example `site///index` will be converted into `site/index` Default: `true`. | `UrlNormalizer` |
| <span id="property-normalizeTrailingSlash"></span>`$normalizeTrailingSlash` | `bool` | Whether trailing slash should be normalized according to the suffix settings of the rule Default: `true`. | `UrlNormalizer` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`normalizePathInfo()`](#method-normalizePathInfo) | Normalizes specified pathInfo. | `UrlNormalizer` |
| [`normalizeRoute()`](#method-normalizeRoute) | Performs normalization action for the specified $route. | `UrlNormalizer` |
| [`collapseSlashes()`](#method-collapseSlashes) protected | Collapse consecutive slashes in $pathInfo, for example converts `site///index` into `site/index`. | `UrlNormalizer` |
| [`normalizeTrailingSlash()`](#method-normalizeTrailingSlash) protected | Adds or removes trailing slashes from $pathInfo depending on whether the $suffix has a trailing slash or not. | `UrlNormalizer` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-ACTION_REDIRECT_PERMANENT"></span>`ACTION_REDIRECT_PERMANENT` | `301` | Represents permament redirection during route normalization. | `UrlNormalizer` |
| <span id="const-ACTION_REDIRECT_TEMPORARY"></span>`ACTION_REDIRECT_TEMPORARY` | `302` | Represents temporary redirection during route normalization. | `UrlNormalizer` |
| <span id="const-ACTION_NOT_FOUND"></span>`ACTION_NOT_FOUND` | `404` | Represents showing 404 error page during route normalization. | `UrlNormalizer` |

## Property details

### $action <span id="property-action"></span>

```php
public int|callable|null $action = self::ACTION_REDIRECT_PERMANENT
```

Action to perform during route normalization.

Available options are:
- `null` - no special action will be performed
- `301` - the request should be redirected to the normalized URL using
  permanent redirection
- `302` - the request should be redirected to the normalized URL using
  temporary redirection
- `404` - [`NotFoundHttpException`](/docs/api/yii/web/notfoundhttpexception/index.md) will be thrown
- `callable` - custom user callback, for example:

  ```
  function ($route, $normalizer) {
      // use custom action for redirections
      $route[1]['oldRoute'] = $route[0];
      $route[0] = 'site/redirect';
      return $route;
  }
  ```

## Method details

### normalizePathInfo() <span id="method-normalizePathInfo"></span>

```php
public function normalizePathInfo(string $pathInfo, string $suffix, bool &$normalized = false): string
```

Normalizes specified pathInfo.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$pathInfo` | `string` | PathInfo for normalization |
| `$suffix` | `string` | Current rule suffix |
| `$normalized` | `bool` | If specified, this variable will be set to `true` if $pathInfo was changed during normalization |

**Returns**

`string`: Normalized pathInfo

### normalizeRoute() <span id="method-normalizeRoute"></span>

```php
public function normalizeRoute(array $route): array
```

Performs normalization action for the specified $route.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$route` | `array` | Route for normalization |

**Returns**

`array`: Normalized route

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if invalid normalization action is used.
- [`UrlNormalizerRedirectException`](/docs/api/yii/web/urlnormalizerredirectexception/index.md): if normalization requires redirection.
- [`NotFoundHttpException`](/docs/api/yii/web/notfoundhttpexception/index.md): if normalization suggests action matching route does not exist.

### collapseSlashes() <span id="method-collapseSlashes"></span>

```php
protected function collapseSlashes(string $pathInfo): string
```

Collapse consecutive slashes in $pathInfo, for example converts `site///index` into `site/index`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$pathInfo` | `string` | Raw path info. |

**Returns**

`string`: Normalized path info.

### normalizeTrailingSlash() <span id="method-normalizeTrailingSlash"></span>

```php
protected function normalizeTrailingSlash(string $pathInfo, string $suffix): string
```

Adds or removes trailing slashes from $pathInfo depending on whether the $suffix has a
trailing slash or not.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$pathInfo` | `string` | Raw path info. |
| `$suffix` | `string` |  |

**Returns**

`string`: Normalized path info.