Can you fix my REGEX code? (301 redirect 404)

BR0C0LLI

Newbie
Joined
Nov 18, 2022
Messages
10
Reaction score
1
After web migration and new URL architecture, I need to redirect old URLs because I am getting lot of 404.

Old URL examples:
domain.com/blog/vse-o-produktech/395-vybaveni-a-doplnky-pro-vasi-pergolu
domain.com/blog/71-prave-realizujeme/464-kdo-nainstaloval-pergolu-dominiku-haskovi-jagr

New URL:
domain.com/vybaveni-a-doplnky-pro-vasi-pergolu
domain.com/kdo-nainstaloval-pergolu-dominiku-haskovi-jagr

So red parts need to be removed.


I have tried put this REGEX to my .htaccess:
blog/([A-Za-z0-9]+(-[A-Za-z0-9]+)+)/[0-9]+-
like this:
<IfModule mod_rewrite.c>

RewriteCond %{THE_REQUEST} ^GET\ /blog/([A-Za-z0-9]+(-[A-Za-z0-9]+)+)/[0-9]+-
RewriteRule ^blog/([A-Za-z0-9]+(-[A-Za-z0-9]+)+)/[0-9]+- $1 [L,R=301]

</IfModule>

But it does not work. In online regex tester tools (like regextester.com) it works.
 
this is from chatgpt:


You can use the 301 redirect to redirect old URLs to the new ones. You can add the following code in your .htaccess file:

Redirect 301 /blog/vse-o-produktech/395-vybaveni-a-doplnky-pro-vasi-pergolu https://domain.com/vybaveni-a-doplnky-pro-vasi-pergolu
Redirect 301 /blog/71-prave-realizujeme/464-kdo-nainstaloval-pergolu-dominiku-haskovi-jagr https://domain.com/kdo-nainstaloval-pergolu-dominiku-haskovi-jagr
This will redirect the old URLs to the new ones, and search engines will understand that the page has moved permanently, which will help in maintaining your search engine rankings.
 
I am not neither...

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^blog/[0-9]+-[a-z-]+/(\d+-[a-z-]+)$ /$1 [R=301,L]
</IfModule>
Nice we are so close! It is redirecting me to: /464-kdo-nainstaloval-pergolu-dominiku-haskovi-jagr
Get rid of 464- and I can applicate it on the second case!
 
It seems like the previous regex does not work with your first URL.
You might try this one:
Code:
RewriteRule ^blog/(?:[0-9]+-)?[a-z-]+/\d+-([a-z-]+)$ /$1 [R=301,L]
 
Guys you are fantastic!! Thank you so much, I have been solving this problem for hours! Very appreciate your help
 
Back
Top