How can I setup .htaccess to redirect any URL that ends with 10 digits to home?

For example, I want to redirect these URLs:

  • example.com/abc/1234567890 -> example.com
  • example.com/xyz/0001112223 -> example.com
  • example.com/foo/bar -> ignored

Using mod_rewrite in .htaccess:

RewriteEngine On
RewriteRule \d{10}$ / [R=301,L]

The RewriteRule pattern (regex):
- \d is the shorthand character class for a digit 0-9.
- {10} is for 10 of the preceding pattern (ie. 10 digits).
- $ is an anchor for the end of the string - these digits must appear only at the end of the URL-path.

Whether you should perform a redirect like this is another matter. Multiple redirects to the homepage are likely to be seen as a soft-404 anyway.

  • 1
    Made it into a list. I find that regex is very hard when just start, IMO this is a bit easier to understand, smaller chunks :) (also trying to create a regex-explained-format-precedent)
    – Martijn
    Jan 4 '17 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.