1

In the past there must have been an error in the generation of links all around my website.

Now I want to redirect all the malformed links that are still used by people and which lead to 404 to the correct page.

An example of a wrong url:

http://www.example.com/foo/bar/www.example.com/bar/foo

The correct url would be /bar/foo, so I have to check if the url contains the name of my website: www.example.com and if it does, it should redirect to the rest /bar/foo.

Static example: If I would write all of them individually instead of using regex

RewriteRule ^foo/bar/www.example.com/bar/foo/ /bar/foo [R=301,L]

I have spent quite some time of here reading about regex etc. but I didn't find a solution for my problem.

Thanks in advance.

dombg
  • 271
  • 2
  • 14
  • 1
    Try `RewriteRule ^.+/.+/www.example.com/(.+)/?$ /$1 [R=301,L]` ? – Amit Verma May 10 '19 at 13:16
  • @starkeen: Could you explain your approach pls. What does `/$1` exactly do? How could that be string after `www.example.com`. Thanks – dombg May 10 '19 at 13:21
  • Dom1337 please read the official mod rewrite guide . https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html . – Amit Verma May 10 '19 at 14:16
  • 1
    Okay now it makes total sense. Thank you! For others interested: `RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9). $1 to $9 provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions. $0 provides access to the whole string matched by that pattern.` – dombg May 13 '19 at 13:53

0 Answers0