# Redirect URLs With Capital Letters to Lowercase

> Permanently redirects addresses typed or linked with capital letters, such as /About-Us/, to their lowercase form, avoiding duplicate URLs and broken links.

- Type: PHP
- Runs: Run everywhere
- Category: [Redirects](https://wpide.com/library/category/redirects/)
- Tags: urls, lowercase, seo
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/lowercase-url-redirect/

## Code

```php
add_action( 'template_redirect', function () {
    $uri  = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
    $path = (string) wp_parse_url( $uri, PHP_URL_PATH );

    if ( '' === $path || ! preg_match( '/[A-Z]/', preg_replace( '/%[0-9A-Fa-f]{2}/', '', $path ) ) ) {
        return;
    }

    $lower = preg_replace_callback( '/(%[0-9A-Fa-f]{2})|([A-Z]+)/', function ( $m ) {
        return '' !== $m[1] ? $m[1] : strtolower( $m[2] );
    }, $path );

    if ( null === $lower || $lower === $path ) {
        return;
    }

    $query  = (string) wp_parse_url( $uri, PHP_URL_QUERY );
    $scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
    $host   = wp_parse_url( home_url(), PHP_URL_HOST );
    $port   = wp_parse_url( home_url(), PHP_URL_PORT );

    wp_safe_redirect( $scheme . '://' . $host . ( $port ? ':' . $port : '' ) . $lower . ( '' !== $query ? '?' . $query : '' ), 301 );
    exit;
}, 1 );
```

## Notes

Encoded characters such as %C3%A9 are left as they are. Query strings are kept unchanged.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect URLs With Capital Letters to Lowercase" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
