0

I have a dozen links from an old website I need to redirect to a new path. This is the old URL buildup:

/news/item/{id}-{name}

Which needs to be redirected to:

/blog/item/{id}-{name}

Now I hear you thinking, just swap the news with the blog in your redirect. Well that could be if it was that simple, but as you can imagine, the old News category contained also the news items, and because it's an old website they never split these items into different category's. So I need to identify when I can redirect it to the blog page. Luckily every blog post contains a static string, which we can use to identify the url (I think).

So you have the following URLS:

/news/item/{id}-{name}
/blog/item/{id}-{name}

The static word that is in the {name} of the url is blogpost-name , but can I use that to identify the URLS to redirect to the /blog/ page?

2 Examples:

/news/item/33018-It-was-a-great-day-to--ride
/news/item/29110-Blogpost-author-john-doe-time-to-come-apart

Here we got 2 items in the news url, but only 1 of them, the actual blogpost can only be switched. The other URL needs to stay intact and working.

Kuubs
  • 1,300
  • 1
  • 14
  • 42
  • Hhhmm, still not sure why you can't "just swap the news with the blog in your redirect"? Can you give a more specific example. Define in more detail what is allowed in the different parts of the URL, eg. what is `item`, `{id}` and `{name}`? Is `{id}` numeric for example? In your current example, there doesn't appear to be any reason why `/news/item/{id}-{name}` can't be directly mapped to `/blog/item/{id}-{name}`, since "news" and "blog" are the only parts that differ. – MrWhite Apr 08 '17 at 12:21
  • That is true, but because there are also news items in the news url I cannot just simply swap the urls, there are also other items in the news category that are supposed to have that specific URL. See my EDIT. – Kuubs Apr 08 '17 at 12:45
  • Is it literally `Blogpost` in each blog URL? And should the target URL contain "Blogpost" as well? – Olaf Dietsche Apr 08 '17 at 13:23
  • Is "Blogpost" the "static string" you mentioned? Certainly, if there is anything unique that identifies these URLs then you have a _pattern_ that can be used to construct a redirect. – MrWhite Apr 08 '17 at 13:28
  • Yes, Blogpost-john-doe is the static string in my example – Kuubs Apr 08 '17 at 15:12

1 Answers1

1

To redirect all blog posts from /news/... to /blog/..., except those being "news", you must identify them by "-Blogpost-", capture the part you want to keep (...) and reuse this in the target URL $1

RewriteRule ^news/(item/\d+-Blogpost-.+)$ /blog/$1 [R,L]

When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198