【问题标题】:Redirect root to CodeIgniter app in subdir, avoiding infinite loop将根目录重定向到子目录中的 CodeIgniter 应用程序,避免无限循环
【发布时间】: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


    【解决方案1】:

    在/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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-30
      • 2015-12-01
      • 2010-12-09
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多