如何使用 mod_rewrite 将子页面/目录重定向到父目录?
因此,我为一堆我想要重定向到其父目录的页面设置了一系列重定向(例如 /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
So I have a whole series of redirects in place for a bunch of pages I want redirected to their parent directory (e.g. /faq/question1/ -> /faq/), but it's very inflexible, as they're manually generated.
How can I set up a RegEx-fueled RewriteRule
for this? I haven't been able to figure it out, and would dearly love some guidance. Here's the whole of my .htaccess, to avoid any conflicts (you can see the individual RewriteRules
I have in place right now):
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要重定向所有以
/faq/
作为前缀的 URL 路径的请求,可以使用以下规则:单个
.
代表任意字符。因此,每个 URL 路径都以/faq/
开头,后跟至少一个字符的请求将被重定向到/faq/
。If you want to redirect all requests that’s URL paths have
/faq/
as prefix, you can use this rule:The single
.
represents an arbitrary character. So every request that’s URL path starts with/faq/
that followed by at least one character is redirected to/faq/
.