2

我有这个网址结构:

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网址,因为我想要网址格式(保留“管理员”字样):

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 的故障排除,您可以在下面找到我的 dir 结构

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

文档根 .htaccess

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

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

更新>工作代码

好吧,在@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]
4

2 回答 2

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>
于 2012-12-04T19:07:57.237 回答
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>
于 2012-12-04T20:47:50.427 回答