2

I`m trying to convert standard 301 redirects using regexp after several changes of categories structures.

How to do this with regular expression?

Old -> New
http://www.example.com/cat1/cat2/cat3/bla1-bla2-bla3-machine-ab123 -> http://www.example.com/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
http://www.example.com/cat1/cat2/cat3/bla4-bla5-bla6-machine-123   -> http://www.example.com/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
http://www.example.com/cat4/cat5/bla7-bla8-bla9-machine-123        -> http://www.example.com/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123

Now I use this:

Redirect 301 /cat1/cat2/cat3/bla1-bla2-bla3-machine-ab123 http://www.example.com/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
Redirect 301 /cat1/cat2/cat3/bla4-bla5-bla6-machine-123   http://www.example.com/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
Redirect 301 /cat4/cat5/bla7-bla8-bla9-machine-123        http://www.example.com/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123

I want to realize next algorithm: If URL contain machine-ab123 or machine-123 at end of url and full url do not equal final version of url /cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123 rewrite it to final version /cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123

Looking on previous answer for me (https://webmasters.stackexchange.com/a/98981/69774) I made such contruction:

RewriteCond %{REQUEST_URI} /(machine-ab123|machine-123)$
RewriteCond %{REQUEST_URI} !^/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
RewriteRule /(machine-ab123|machine-123)$ /cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123 [R=301,L]

Not working, where could be mistake?

By the way, if I need to check does URL contain machine-ab123 or machine-123 not only at end of url, but also in any position, for example,

/cat11/cat22/cat33/bla11-machine-ab123-bla22-bla33
/cat11/cat22/cat33/bla11-bla22-machine-ab123-bla33

how regexp should be changed?

Pretzel
  • 73
  • 4

1 Answers1

1

I found a solution, may will be useful for somebody.

If searched substring located at the end of URL:

RewriteCond %{REQUEST_URI} ^(.*)(machine-ab123|machine-123)$
RewriteCond %{REQUEST_URI} !^/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
RewriteRule ^(.*)(machine-ab123|machine-123)$ /cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123 [R=301,L]

If searched substring located in the middle of URL:

RewriteCond %{REQUEST_URI} ^(.*)(machine-ab123|machine-123)(.*)$
RewriteCond %{REQUEST_URI} !^/cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123
RewriteRule ^(.*)(machine-ab123|machine-123)(.*)$ /cat11/cat22/cat33/bla11-bla22-bla33-machine-ab123 [R=301,L]
Pretzel
  • 73
  • 4