M
M
miron-partner2018-05-28 21:45:49
Regular Expressions
miron-partner, 2018-05-28 21:45:49

WordPress | How to register a 301 redirect after changing the CNC format?

Hello. The site had many headings and the following link format (NC):
site.ru/category1-post1.html
site.ru/category2-post23.html
site.ru/category3-post17.html
site.ru/category4-post9.html
That is Instead of a traditional "/", but a hyphen "-") instead of a directory separator,
I want to bring everything back to normal - change the CNC and also combine several categories into one. So that in the end it would be like this:
site.ru/category5/post1.html
site.ru/category5/post23.html
site.ru/category5/post17.html
site.ru/category5/post9.html
To save the link and position of all pages of the site, you need to set up a 301 redirect. Need help from knowledgeable people. Help compose entries in the .htaccess file according to the above examples (if you know). Here is my version:
RewriteRule ^(.*)/category1-(.*)$ $1/category5/$2 [R=301,L]
RewriteRule ^(.*)/category2-(.*)$ $1/category5/$2 [ R=301,L]
RewriteRule ^(.*)/category3-(.*)$ $1/category5/$2 [R=301,L]
RewriteRule ^(.*)/category4-(.*)$ $1/category5/ $2 [R=301,L]
Right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
miron-partner, 2018-05-31
@miron-partner

RewriteRule ^category[0-4]-(.+\.html)$  https://site.ru/category5/$2 [R=301,L]

If we want to use a single subexpression taken from the left side (.+\.html) then to insert it on the right side we will already use just $1. And if there were 2 pairs of brackets (2 subexpressions) on the left side, then on the right side we would call them using $1 and $2.
I needed to massively move posts from one category to another. Moreover, my CNC (person-like URL) was configured so that the category in the link was distinguished not as usual by slashes on both sides (/category/), but by a hyphen on the right (/category-). And I had to change the CNC to normal, move the posts to other categories and set up 301 redirects from the old URLs to the new one in order to save the link and age.
http:/site.ru/znaki_zodiaka-muzhchina-rak-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-ryby-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-lev-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-rak-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-oven-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-vesy-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-skorpion-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-oven-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-bliznecy-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-kozerog-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-kozerog-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-lev-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-strelec-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-telec-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-ryby-v-sekse.html
http:/site.ru/znaki_zodiaka-zhenshhina-vodolej-v-sekse.html
http:/site.ru/znaki_zodiaka-muzhchina-vodolej-v-sekse.html

There was a category "Signs of the Zodiac" (/znaki_zodiaka-). Posts from it had to be moved to the category "Blog" with an already normal CNC (/blog/). Here is the post that caused me to get a 301 redirect:
RewriteEngine On
RewriteBase /
RewriteRule ^znaki_zodiaka-(.+\.html)$ /blog/$1 [R=301,L]

But I had many categories of posts from which I needed to demolish the "Blog" (/blog/). Here's how I wrote it down and it worked:
RewriteEngine On
RewriteBase /
RewriteRule ^(muzhchiny|masturbacia|orgazm|oralniy_seks|devushki|seks_v_internete|sluchayniy_seks|seks_igrushki|raznoe|aksessuary|zhizn_bez_seksa|porno_industriya|kontracepcia|libido|recepty|texnika_seksa|exercise|erekcia|eakuliacia|yumor_i_prikoly|otnoshenija|znaki_zodiaka)-(.+\.html)$ blog/$2 [R=301,L]

Please note that in order not to write a new separate rule each time for all the categories whose posts I wanted to transfer (and there are about 20 such categories), I opened the brackets in just one rule (creating a subexpression) and listed them through "|" . And as a result of this, I already had 2 subexpressions on the left side of my rule, as a result of which the second subexpression on the right side was already called using $2 (and not $1 as it was in option 1 ) . I spent a couple of hours until it dawned on me why Option 2 didn’t want to work for me))) Here is a sensible article that helped me figure it out: max22.ru/server/htaccess
I inserted the redirect code almost at the very top of the file before any other entries with other redirects (I have Wordpress). At the end of the file, these rules didn't want to work. I read that rules are processed in the order in which they are written in .htaccess. It is possible that other rules (which were higher) somehow interrupted my new ones. I inserted mine at the beginning and it worked.
Thank you for participating in solving my problem to Victor Taran Victor Taran and dodo512 dodo512 .

V
Viktor Taran, 2018-05-29
@shambler81

RewriteRule ^category[0-4]-(.+\.html)$  https://site.ru/category5/$2 [R=301,L]

You can use one rule.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question