1

I'm trying to turn a string of this type:

http://example.com/mypage/272-16+276-63+350-02

where aaa-bb are product codes and their numbers may vary from 2 to anything, but I doubt there will ever be more than 5 into:

http://example.com/mypage/272-16+276-63+350-02/?skus=272-16+276-63+350-02

using a redirect match. I'm fairly new to regular expressions and I don't seem to be able to get the negative lookahead and lookbehind to work the way I want.

To capture the string the first time is fairly easy, I used ([\-\+0-9]+) but I don't want it to match on redirection (when I already have a ? in my link). Using ([\-\+0-9]+)(?!\?)(?<\?) doesn't do the trick, it only excludes my last digit from the match. So, is there a way to make regex consider all my product codes as one block, so I can than check if there is a question mark before or after it?

Thank you for looking into this.

elixenide
  • 44,308
  • 16
  • 74
  • 100
tao
  • 82,996
  • 16
  • 114
  • 150
  • 1
    What is the language/technology that you are using that you use this regex for? – nhahtdh Jan 14 '14 at 19:06
  • 1
    Really not sure what you're asking, can you clarify the question and provide succinct examples and what you want matched? – brandonscript Jan 14 '14 at 19:06
  • @nhahtdh: It's a WP website, this goes into .htaccess – tao Jan 14 '14 at 19:08
  • @remus: basically I want to put redirect from first link to second using a redirect match without having an infinite redirect loop. – tao Jan 14 '14 at 19:10
  • Does the RegEx actually need to *validate* that all product codes are the same format as well? ie. `272-16+2-63+350-02` would *not* match because the second product's first digits are not 3 in length. – tenub Jan 14 '14 at 19:12
  • Still doesn't help. See my first comment. – brandonscript Jan 14 '14 at 19:14
  • @tenub no, it doesn't. and the skus can be with or without dash. they are devided by + signs and the dash separates the parent id from variation id. `1234+5679+123-1234+3-14` is another example of matched string. – tao Jan 14 '14 at 19:16
  • I think you need a `RewriteCond` to check whether Query string is there or not before applying the rule. – nhahtdh Jan 14 '14 at 19:16
  • He wants to manipulate `http://example.com/mypage/272-16+276-63+350-02 ` to read as `http://example.com/mypage/272-16+276-63+350-02/?skus=272-16+276-63+350-02`, but if the string being checked is already `http://example.com/mypage/272-16+276-63+350-02/?skus=272-16+276-63+350-02` it should not manipulate it further when the RegEx checks it. Basically need to do a `preg_replace` on the string with a condition that no `/?` exists at the end of the match, or simply check for query string. – tenub Jan 14 '14 at 19:16
  • @remus: i want to capture all +,-,and digits into one group and than I want to look if there is any question mark before or after that group. If there is not question mark, I match, if there is, I don't match. I hope this clarifies what I want to do. – tao Jan 14 '14 at 19:18
  • This `(?<\?)` is not a valid assertion `([\-\+0-9]+)(?!\?)(? <-- Quantifies nothing <\?)` –  Jan 14 '14 at 19:34
  • @nhahtdh following your suggestion, I looked up some RewriteCond resources to learn how to do that. I came up with `RewriteCond %{QUERY_STRING} !skus= RedirectMatch 301 /mypage/([\-\+0-9]+) /mypage/$1?skus=$1` Isn't this supposed to work? It still gives me a redirection loop. – tao Jan 14 '14 at 19:48

2 Answers2

1

You can't mix mod_rewrite (RewriteCond) and mod_alias (RedirectMatch) together. You need to stick with one or the other and you can't match the query string with a RedirectMatch, so you're using mod_rewrite:

RewriteEngine On
RewriteCond %{QUERY_STRING} !skus=
RewriteRule ^mypage/([\-\+0-9]+)$ /mypage/$1?skus=$1 [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

Maybe try (?<=http://example.com/mypage/)[0-9+-]+$ Will match only the first case.

tenub
  • 3,386
  • 1
  • 16
  • 25