【发布时间】:2014-09-08 17:04:41
【问题描述】:
我目前在我的 htdocs 根目录中部署了以下 .htaccess:
RewriteEngine On
# Map / to /foo.
RewriteRule ^$ /foo/ [L]
# Map /x to /foo/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/foo/
RewriteRule ^(.*)$ /foo/$1
这会将 example.com/foo/whatever 重定向到 example.com/whatever,除非它与其他应用程序所在的其他目录匹配(狐狸示例 example.com/admin/whatever,应该保持这种方式)。这部分行为是需要的。但是现在我在 foo 上的每个页面都有两个 url,我有 example.com/foo/whatever 和 example.com/whatever。我想为从 /foo/x 到 /x 的所有请求进行 301 重定向,但如果我添加
# Redirect /foo/x to /x
RewriteRule ^foo/(.*)$ /$1 [R=301,L]
到 .htaccess 我显然得到了一个重定向无限循环。我怎样才能达到我想要的?提前感谢您提供的任何帮助。
【问题讨论】: