.htaccess用index.html重写目录到子目录

时间:2013-09-07 22:00:47

标签: .htaccess

在网站上有一些带有html文件的目录,如:

site.com/folderone/index.html
site.com/foldertwo/index.html

它可以通过以下链接获得:

site.com/folderone
site.com/foldertwo

现在所有这些目录都移到了/ old-pages:

site.com/old-pages/folderone/index.html
site.com/old-pages/foldertwo/index.html

但旧链接应该可用,所以.htaccess文件:

RewriteEngine on
RewriteRule ^folde(.*)$ /old-pages/folde$1 [L]

重写正确 site.com/folderone / site.com/folderone/index.html

问题是:对于 site.com/folderone ,它不会重写,而是重定向到 site.com/old-pages/folderone /

1 个答案:

答案 0 :(得分:1)

重定向可能是因为mod_dir和DirectorySlash指令而发生的,该指令重定向对缺少尾部斜杠的目录的请求,以便在那里使用尾部斜杠。有一个的原因,为什么会发生这种情况,因为有一个信息泄露安全问题没有尾随斜杠。

如何避免重定向,请将其关闭(不推荐):

DirectorySlash Off

或通过mod_rewrite包含斜杠,以便两个模块不会在同一请求中相互干扰:

RewriteRule ^folder([^/]+)$ /folder$1/ [L,R=301]

您必须在其他重写规则之前添加该规则,以便首先应用它。然后你的另一条规则应该有效。