.htaccess重定向子文件夹

时间:2011-09-24 18:03:07

标签: .htaccess mod-rewrite redirect

嗨,我不是想象中的程序员,而是在我的htaccess文件中尝试基于以下内容进行多重301重定向:

所以我有大量的网址都有类似的命名约定 - 这里是2的样本。

http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/

这些网址需要重定向到:

http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/

我可以一次将它们重定向到一行,但我想使用正则表达式。

这是我到目前为止所想的但不起作用:

RewriteRule ^ garrett - ([a-z])/ $ / garrett-metal-detectors / $ 1 / [R]

基本上我需要重定向以“garrett-”开头的根目录下的任何页面,以包含“garrett-metal-detectors”的文件夹路径。

任何想法都会受到很多赞赏。非常感谢您的帮助。

2 个答案:

答案 0 :(得分:6)

如果您想要临时重定向使用:

RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]

如果您想要永久重定向使用:

RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]

答案 1 :(得分:0)

我不是正则表达式的专家,但看起来你的注册表可能有点不合适......

尝试:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]

这是以“garrett”开头,后跟任何字母/数字/连字符组合的任何内容。

注意:在目标部分中使用“garett”会给您一个重定向循环,因此您可能需要选择一个不同的单词,或者将它们全部删除...

相关问题