.htaccess,双重定向评估uri

时间:2012-12-04 17:11:45

标签: php .htaccess mod-rewrite

我有这个网址结构:

proyectos.example.com/ci-doctrine/index.php
proyectos.example.com/ci-doctrine/index.php/welcome
proyectos.example.com/ci-doctrine/index.php/blog
proyectos.example.com/ci-doctrine/admin.php
proyectos.example.com/ci-doctrine/admin.php/welcome
proyectos.example.com/ci-doctrine/admin.php/dashboard

对于index.php网址,我想保留以下格式:

proyectos.example.com/ci-doctrine/    //default index view
proyectos.example.com/ci-doctrine/welcome
proyectos.example.com/ci-doctrine/blog

所以,我的.htaccess看起来像:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

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

上面的.htaccess代码运行正常。问题存在于admin.php网址,因为我希望网址格式为(保留'admin'字样):

proyectos.example.com/ci-doctrine/admin
proyectos.example.com/ci-doctrine/admin/dashboard
proyectos.example.com/ci-doctrine/admin/welcome

然后,我编辑了我的htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

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

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

但是当我浏览proyectos.example.com/ci-doctrine/admin时,我收到了500错误,所以我认为我的管理文件规则不能正常运行。

日志

我查看了error_log,每次尝试访问proyectos.example.com/ci-doctrine/admin时都会打印一条消息:

  

由于可能的配置错误,请求超出了10个内部重定向的限制。如有必要,使用'LimitInternalRecursion'增加限制。使用“LogLevel debug”获取回溯。

后面是重定向列表:

[Wed Dec 05 09:47:07 2012] [error] [client 201.192.30.254] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Wed Dec 05 09:47:07 2012] [debug] core.c(3112): [client 201.192.30.254] r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/proyectos/index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /index.php/admin
[Wed Dec 05 09:47:07 2012] [debug] core.c(3118): [client 201.192.30.254] redirected from r->uri = /ci-doctrine/admin

目录结构

对于porpuses的疑难解答,您可以在下面找到我的目录结构

public_html/
    .htaccess/
    index.php
    proyectos/
        ci-doctrine/
            .htaccess
            index.php
            admin.php

文件根.htaccess

在根目录中,我在子域proyectos.example.com/ci-doctrine中安装了我的应用程序,因此我对index.php应用了相同的规则。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

更新&gt;工作代码

嗯,在@anubhava的帮助下,我得到了这项工作:

文件根.htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

子域名.htaccess:

# If the user types "index.php" or "admin.php".
RewriteCond $1 !^(index\.php|admin\.php|images|robots\.txt)

# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ admin\.php [L,QSA]

# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ admin\.php/$1 [L,QSA]

# If the user types any site section, like "site/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]

2 个答案:

答案 0 :(得分:1)

试试这个:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

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

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

答案 1 :(得分:1)

实际上问题是你的第一条规则中有一个额外的(和不正确的)RewriteCond。以下是适合您的经过修改的正确代码:

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

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

编辑:您的$DOCUMENT_ROOT/.htaccess应该像这样修改:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  RewriteCond %{HTTP_HOST} ^(www\.)?rootdomain\.com [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>