2009-05-29 52 views
4

自从我与.htaccess混淆了一段时间后,我似乎无法完全理解这一点。我有一个网站,例如example.com,我希望example.com/*可以重定向到example.com/collector.html ,除了,我想继续使用子目录example.com/special下的网址。如何使用htaccess重定向除一个子目录以外的所有东西

例如:

  • example.com/page.html应该重定向到example.com/collector.html
  • example.com/foo/bar应该重定向到example.com/collector.html
  • example.com/special/page.html不应重定向

我本来以为像

RedirectMatch 302 ^/[^(special)]/.* /collector.html 
RedirectMatch 302 ^/[^(collector.html)/]* /collector.html 

会做的伎俩,但它似乎并没有按照我想要的方式工作。

回答

4

也许这? (需要mod_rewrite)

RewriteEngine On 
RewriteRule !^(special(/.*)?|collector\.html)$ collector.html [R,L] 
0

试试这个:

RedirectMatch 302 /\/[^(special)]\/.+/ /collector.html 
RedirectMatch 302 /\/[^(collector\.html)]/ /collector.html 
+0

这可以使所有子目录转到收集器,但不起作用来重定向根目录中的文件,例如, example.com/page.html – 2009-05-29 13:40:16

+0

会发生什么?没有任何重定向,页面是否正确检索? – 2009-05-29 14:04:44

+0

试试这个在同一行:RedirectMatch 302 /\/[^(special\//)|(collector\.html)].*/ /collector.html – 2009-05-29 14:07:31

0

也许......?

RewriteEngine on 
RewriteRule ^(?!special) collector.html [R,NC] 
2

这对我在Apache 2.2.13上的工作很好,我重新创建了你描述的目录结构。

RedirectMatch 302 /(?!special) http://www.example.com/collector.html 

括号内的模式表示如果URI包含字符串'special',则不匹配。

此外mod并不总是可用的,在这种情况下是不需要的。

相关问题