将root重定向到subdir中的CodeIgniter应用程序,避免无限循环

时间:2014-09-08 18:46:36

标签: regex apache .htaccess mod-rewrite

我目前在CodeIgniter应用程序的目录中部署了以下.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

以及我的htdocs root中的以下.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/ww以及example.com/whatever,除非它与其他应用程序所在的其他目录相匹配(例如,例如:/ appmin / 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。我怎样才能实现我的目标?提前感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

/foo/.htaccess中使用此新规则将/foo/abc重定向到/abc

RewriteEngine On

RewriteCond %{THE_REQUEST} /foo/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]