【发布时间】:2014-11-02 01:46:13
【问题描述】:
我目前在我的 CodeIgniter 应用的目录中部署了以下 .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
以及我的 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
foo 是我的 CodeIgniter 应用程序所在的目录。这会将 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 我显然得到了一个重定向无限循环。 The solution described here for a related question 不起作用,因为它不会在需要时重定向到 index.php。我怎样才能达到我想要的?提前感谢您提供的任何帮助。
【问题讨论】:
标签: regex apache .htaccess mod-rewrite