I have a rather odd situation where our old URLs are ending in .-detail and need changing to just -detail.

For example:

  • example.com/product.-detail needs changing to example.com/product-detail

In other words the . needs removing.

I figured a simple 301 rule would do the trick but I'm having trouble getting the condition string to pass

RewriteRule ^$.-detail https://www.example.com/$1-detail [R=301,L]

What would the correct way of doing this be please?

1 Answer 1

RewriteRule ^$.-detail https://www.example.com/$1-detail [R=301,L]

There are few issues with the regex:

  • The $ (end-of-string anchor) is in the wrong place. This should be at the end of the regex (you want to match .-detail at the end of the URL-path, ie. \.-detail$).
  • There is no capturing subpattern, so the $1 backreference is always going to be empty.
  • The literal dot should be backslash-escaped, otherwise this matches any character and would result in a redirect-loop (if there was a capturing subpattern as mentioned above) - since it would also match the redirected URL, reducing it by 1 character at a time until there's nothing left of the URL!

The RewriteRule pattern (first argument) should be more like this:


The ^ (start-of-string anchor) is not required since regex is greedy by default and the dot matches "everything". (Although you could include it if it makes it easier for you to read.)

The $1 backreference then contains the part of the URL-path matched by the parenthesized subpattern (.+).

You could capture both parts, before and after the dot, to save repeating the literal string in the substitution. For example:

RewriteRule (.+)\.(-detail)$ https://www.example.com/$1$2 [R=301,L]

The $2 backreference simply contains the string -detail, as captured from the second capturing group in the RewriteRule pattern.

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.