301重定向删除第一个目录

时间:2015-05-08 19:37:21

标签: apache .htaccess mod-rewrite rewrite http-status-code-301

我有几个网址我想从网址中删除整个directoy部分。 htaccess 301重定向会是什么?有人可以帮忙。

例如

https://example.com/first-lastname-word1-word2/biography/firstnamelastname
https://example.com/first-lastname-word1-word2/activity/firstnamelastname
https://example.com/first-lastname-word1-word2/announcements/firstnamelastname
https://example.com/first-lastname-word1-word2/wall/firstnamelastname

成为https://example.com/activity/firstnamelastname https://example.com/biography/firstnamelastname等等。

1 个答案:

答案 0 :(得分:2)

使用mod_rewrite将此规则非常优先的第一条规则放在RewriteEngine行的下方:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(memorial-photos|videos|images|category)/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([^/]+/[^.]+)$ /$1 [L,NE,R=301]