RewriteRule rule in .htaccess for matching keyword

Viewed 199

I need to make a 301 redirect from

https://www.example.net/sub1/specific_keyword/page1/page2
https://www.example.net/sub2/sub3/specific_keyword/page3/page4
https://www.example.net/specific_keyword/page5/page6

to

https://www.example.net/sub1/
https://www.example.net/sub2/sub3/
https://www.example.net/

I tried this code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)\/specific_keyword$ "https\:\/\/www\.example\.net\/$1" [R=301,L]

But no luck.

1 Answers

With your RewriteRule attempt, you demanded that the requested URL ends with /folder-to-remove, via the $ at the end, so that won’t match (Source: #comment120998408_68464303)

With that fixed, place following rules at top of your htaccess Rules file. Make sure to clear your browser cache before testing your URLs or use a redirect checker online.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule ^([^/]*)/([^/]*)/.*$ $1/$2? [R=301,NE,L]

OR only match the specific keyword

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)specific_keyword/ https://www.example.com/$1 [R=301,L,NE]
Related