redirection-regex-en

About “Regex” in Redirections

In this article, you’ll learn each component of regular expressions ( Regex ) for Redirection, along with common usage examples.
For information about redirects, please also refer to Manage 301 redirects by Redirection

 

About “Regex” in Redirections

1. Explanation about “Regex”

Actual Conversion Example

  • Before: “Date and Post Name” https://taracotapas.com/2026/01/20/sample-post/

  • After: “Post Name” https://taracotapas.com/sample-post/

Part-by-Part Explanation

Source URL: ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)

① ^/ “URL must start with /”

  • ^ → Beginning of URL
  • / → Immediately followed by a slash

② ([0-9]{4}) = Year (YYYY) → Captured in $1

  • [0-9] → Digits 0-9
  • {4} → 4 digits
  • () → Capture group

③ /([0-9]{2}) = Month (MM) → Captured in $2

  • {2} → Two-digit number

④ /([0-9]{2}) = Day (DD) → Captured in $3

⑤ /(.*) = Slug portion (super important)

  • . → Any single character
  • * → Zero or more characters
  • (.*) → Captures everything together

Capture Correspondence

Group Content
$1 Year (YYYY)
$2 Month (MM)
$3 Day (DD)
$4 Slug (Article Title)

Target URL: /$4
Meaning

  • Discard the year, month, and day ($1 $2 $3) entirely
  • Keep only the slug portion
  • $4 includes the trailing /, so it’s not needed here

Actual Conversion Example

  • Before: “Date and Post Name” https://taracotapas.com/2026/01/20/sample-post/
  • After: “Post Name” https://taracotapas.com/sample-post/

2. Basics of Redirection “Regex”

Basic Parts

  • (.*) ← Captures everything
  • ([0-9]+) ← Numbers
  • ^ / $ ← Start/End

Redirection Operation Rules

  • Regex ON
  • Ignore Slash / Ignore Case are OFF by default
  • Source / Target are relative paths
  • Use 301 (permanent) redirects
  • Order rules from broadest to narrowest

3. Commonly Used “Regex” Examples

① URL with year/month/day → Slug only

  • Purpose: WordPress permalink change (/%year%/%month%/%day%/ → /%postname%/)
  • Source: ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)
  • Target: /$4

② Move an entire directory structure to a different location

  • Purpose: /old-blog/xxx → /blog/xxx
  • Source: ^/old-blog/(.*)
  • Target: /blog/$1

③ Japanese URL → English slug (with sequential number)

  • Purpose: Organize Japanese/English URLs
  • Source: ^/ja/Japanese-title-([0-9]+)
  • Target: /ja/english-title-$1

④ Remove index.php / index.html

  • Purpose: SEO normalization / rescue old URLs
  • Source: ^/(.*)/index\.(php|html?)$
  • Target: /$1/

⑤ Remove trailing slash → Add trailing slash

  • Purpose: URL standardization (prevent SEO issues)
  • Source: ^(.+[^/])$
  • Target: $1/

⑥ http → https

  • Purpose: Redirecting traffic after SSL migration
  • Source: ^http://(.*)
  • Target: https://$1

⑦ Upper-case URL → Lower-case URL

  • Purpose: When external incoming URLs are messy
  • Source: ^/(.*[A-Z].*)
  • Target: /$1

⑧ Redirect while ignoring query URLs

  • Purpose: Ignore ?utm_source=xxx
  • Source: ^/some-page/?$
  • Target: /some-page/

⑨ Bulk rescue for URLs generating massive 404s

  • Purpose: Search Console compliance
  • Source: ^/tag/(.*)
  • Target: /blog/

⑩ Catch both ja and en (multilingual)

  • Purpose: When using Polylang / WPML
  • Source: ^/(ja|en)/(.*)
  • Target: /$1/new-path/$2

About “Regex” in Redirections

Related Articles