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?

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]
  • 2
    As just mentioned in a comment on your other question, the first RewriteCond directive on both your rules is not required here. You are already performing this check in the RewriteRule pattern - which is more efficient. Also, the regexp can be tidied... to match something anywhere in the string (URL) you can simply use the regexp something (no anchors or special meta chars required). There is no need for the more "complex" ^(.*)something(.*)$. – MrWhite Sep 11 '16 at 14:50

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.