如何使用mod_rewrite将子页面/目录重定向到父目录?

时间:2010-04-16 14:54:35

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

所以我有一系列重定向到一堆我想要重定向到他们父目录的页面(例如/ faq / question1 / - > / faq /),但是它非常不灵活,因为它们是手动的生成。

如何为此设置一个以RegEx为燃料的RewriteRule?我无法弄明白,并且非常喜欢一些指导。这是我的整个.htaccess,以避免任何冲突(你可以看到我现在所拥有的个人RewriteRules):

AddDefaultCharset utf-8
AddHandler php5-script .php
DirectoryIndex index.php

ErrorDocument 403 /errors/forbidden.html
ErrorDocument 500 /errors/internalerror.html

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^halftoneproductions.com
RewriteRule (.*) http://www.halftoneproductions.com/$1 [R=301,L]

# FAQ redirects
RewriteRule  ^faq/how-much-does-it-cost-to-make-an-albummix-a-songetc /faq/ [R=301,L]
RewriteRule  ^faq/im-a-singersongwriter-with-no-band-members-would-you-be-able-to-do-a-full-production-of-my-songs-with-session-musicians /faq/ [R=301,L]
RewriteRule  ^faq/do-you-do-postsound-for-film-at-all /faq/ [R=301,L]
RewriteRule  ^faq/i-have-a-home-studio-and-just-need-to-record-___-is-that-cool /faq/ [R=301,L]
RewriteRule  ^faq/are-you-a-publishing-company /faq/ [R=301,L]
RewriteRule  ^faq/do-you-need-any-interns-or-runners /faq/ [R=301,L]
RewriteRule  ^faq/can-i-bring-my-own-producerengineer-to-a-session /faq/ [R=301,L]
RewriteRule  ^faq/what-types-of-music-do-you-work-on /faq/ [R=301,L]
RewriteRule  ^faq/do-you-work-with-languages-other-than-english /faq/ [R=301,L]
RewriteRule  ^faq/do-you-guys-make-beats /faq/ [R=301,L]
RewriteRule  ^faq/i-have-an-old-tape-reelcasetteminidiscetc-and-would-love-to-transfer-it-to-some-other-form-can-you-help-out /faq/ [R=301,L]
RewriteRule  ^faq/i-have-my-own-producerengineeretc-and-just-need-help-working-out-a-budget-for-my-project-and-booking-studio-time-can-you-help-out /faq/ [R=301,L]

# Recent Work redirects
RewriteRule  ^recent-work/josh-and-neal /recent-work/ [R=301,L]
RewriteRule  ^recent-work/midnights-son /recent-work/ [R=301,L]
RewriteRule  ^recent-work/bearkat /recent-work/ [R=301,L]
RewriteRule  ^recent-work/madi-diaz /recent-work/ [R=301,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

1 个答案:

答案 0 :(得分:5)

如果要将所有URL路径/faq/的请求重定向为前缀,可以使用以下规则:

RewriteRule  ^faq/. /faq/ [R=301,L]

.代表任意字符。因此,每个以/faq/开头且后跟至少一个字符的网址路径的请求都会重定向到/faq/