Sujan Shrestha
Sujan Shrestha

Reputation: 1040

301 redirection regex

I am trying to redirect 301 pages. I came across one issue, in which I am stuck. 1st Page: https://test.example.com/the-magazine/2012-springsummer-issue/about-last-night-event-planning/

For above page i have written the redirect regex as:

Source URL: /the-magazine/2012-springsummer-issue/([A-Za-z].*)
Target URL: https://www.example.com/article/$1

$1 replaces all the text captured by (.*)

I have another page:

2nd Page: https://test.example.com/category/the-magazine/2012-springsummer-issue

This page is also being redirect. How to write a regex to say if '/category/' exists donot redirect to Target URL: https://www.example.com/article/$1

Thank you in advance

Upvotes: 0

Views: 42

Answers (1)

asdf101
asdf101

Reputation: 669

Is this what you want?

expression: (?<!\/category)\/the-magazine\/2012-springsummer-issue\/([A-Za-z].*)

  • make sure there is no \category with a negative lookbehind: (?<!\/category)
  • match literally: \/the-magazine\/2012-springsummer-issue\/
  • capture the rest of the url: ([A-Za-z].*)

Example 1:

https://test.example.com/the-magazine/2012-springsummer-issue/about-last-night-event-planning

Matches /the-magazine/2012-springsummer-issue/about-last-night-event-planning, captures about-last-night-event-planning.

Example 2:

https://test.example.com/category/the-magazine/2012-springsummer-issue/about-last-night-event-planning

Matches nothing, captures nothing

Upvotes: 1

Related Questions