如何指定Apache重写规则以捕获除主页(root和index.html)之外的所有URL

时间:2014-10-15 00:59:06

标签: apache .htaccess mod-rewrite redirect

我的目标是拥有一个' catchall'当用户键入URL时,将fire重定向到通用域/ URL,如:

http://domain.com/blah
http://domain.com/blah/blah2
http://domain.com/blah/blah2/file.pdf
http://domain.com/blah?item=1243

以上所有网址都会重定向到:

http://domain.com/about

基本上,URL可以包含斜杠和查询字符串。如果网址为:

,我也不想要重定向
http://domain.com

http://domain.com/index.html

我的解决方案是将ErrorDocument 404或ErrorDocument 500重定向到http://domain.com/about但我已经被告知我无法执行此操作,因为页面404页面本身正在被跟踪(分析等)。

目前我有以下内容:

RewriteRule ^([A-Za-z0-9-]+)$ http://domain.com/about [NC,R=301]

似乎有效但不适用于上述所有情况。

非常感谢任何帮助,我希望我已经清楚了。

1 个答案:

答案 0 :(得分:1)

问题是您的正则表达式还匹配/about,这会导致重定向循环。

RewriteRule ^(index\.html|)$ - [L]
RewriteRule ^(?!about) %{REQUEST_URI} [L,R=301]

或者,如果您实际拥有的唯一网页是/about/index.html,则可以使用后备资源:

FallbackResource /about