Nested regex "options"
-
stratacorps
-
Topic Author
-
Offline
-
New Member
-
Less
More
-
Posts: 4
-
Thank you received: 0
-
-
7 years 2 months ago #6344
by stratacorps
I am trying to get this regex expression, which works in PHP to work with a multi-url redirect...
I have several urls that need to be redirected to a new, single category.
Old Urls:
/racing-results-news/999-article-title-category-1
/racing-event-news/888-article-title-category-2
New Url:
/event-news-archives/article-title-category-1
This regex works when tested in PHP...
\/(racing-(results|event)-news)(\/\d+)(-)(.*)
replacing ${1} with 'event-news-archives'
replacing ${3} with '/'
replacing ${4} with ${5}
But with the regex expression as my From URL, I'm not getting any hits. So I decided to try and just do separate individual replacements, but I can't get the replacement preg matches to work either.
I tried:
id={preg_match 0}/(racing-results-news|racing-event-news)(/.*){/preg_match}
title={preg_match 1/(racing-results-news|racing-event-news)(/.*){/preg_match}
What am I doing wrong?
Please Log in or Create an account to join the conversation.
Less
More
-
Posts: 3194
-
Thank you received: 441
-
-
7 years 2 months ago #6345
by admin
Hi,
try this:
id={preg_match 2}/\/racing-(results|event)-news\/([0-9]+)-([^\/]+)/i{/preg_match}
title={preg_match 3}/\/racing-(results|event)-news\/([0-9]+)-([^\/]+)/i{/preg_match}
Kind regards,
Luigi
The following user(s) said Thank You: stratacorps
Please Log in or Create an account to join the conversation.
-
stratacorps
-
Topic Author
-
Offline
-
New Member
-
Less
More
-
Posts: 4
-
Thank you received: 0
-
-
7 years 2 months ago #6351
by stratacorps
Yes!
Okay, stupid question. I use PHP to validate my regex (I'm slow to the pattern matching), so in the From URL I don't need to do all of the normal escaping \/ to match the url?
I think that was part of what I was doing wrong...I now have a single rule to account for multiple categories...
From URL: /racing-(results|event)-news/([0-9]+)-([^\/]+)
To URL: //{siteurl}/events-news-archives/${title}?article_id=${id}
Redirect: 301
Request Only: checked
Decode URL: checked
Placeholders: id={preg_match 2}/\/racing-(results|event)-news\/([0-9]+)-([^\/]+)/i{/preg_match}
title={preg_match 3}/\/racing-(results|event)-news\/([0-9]+)-([^\/]+)/i{/preg_match}
I am now using a Regex editor :-/ It will be faster!
Thank you!!
Please Log in or Create an account to join the conversation.
Less
More
-
Posts: 3194
-
Thank you received: 441
-
-
7 years 2 months ago #6352
by admin
The pattern must be specified with this format:
/<regexp>/
Example:
/home/i
So you need to escape the slash if you need it inside the /<regexp>/
Kind regards,
Luigi
Please Log in or Create an account to join the conversation.
Time to create page: 0.164 seconds