.htaccess 中用于匹配关键字的 RewriteRule 规则

RewriteRule rule in .htaccess for matching keyword

我需要从

进行 301 重定向
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

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

我试过这段代码:

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

但运气不好。

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)

修复后,将以下规则放在 htaccess 规则文件的顶部。确保在测试您的 URL 之前清除您的浏览器缓存或使用在线重定向检查器。

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

只匹配特定关键字

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