Score:1

Redirect all URLs ending in ".-detail" to remove the dot

mv flag

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?

Score:0
kz flag
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:

(.+)\.-detail$

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.