【问题标题】:mod_rewrite to remove index.php from Codeigniter in subdirectorymod_rewrite 从 Codeigniter 的子目录中删除 index.php
【发布时间】:2012-05-19 09:51:20
【问题描述】:

Codeigniter 应用程序通常使用 mod_rewrite 从 url 中排除字符串 index.php。我在同一个域中有两个 Codeigniter 应用程序。一个 Codigniter 应用程序位于 web 根文件夹中,另一个 Codigniter 应用程序位于 web 根文件夹的子文件夹中。

Codeigniter 应用程序 1:

http://domain.com/index.php

Codeigniter 应用 2(登陆页面应用):

http://domain.com/land/index.php 

这两个 Codeigniter 应用程序都是原子的,它们之间不共享任何文件。 Codeigniter 框架中的每个文件都在public_html/public_html/land/ 中。所以我需要在寻址根/ 文件夹的url 中排除字符串index.php,并在/land/ 子文件夹中排除字符串index.php

根文件夹中的 .htaccess 文件使用来自 Codeigniter wiki 的广泛推荐的 mod_rewrite 规则(代码如下),这组规则适用于根 Codeigniter 应用程序(应用程序 1)。这些规则位于 web 根文件夹中。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.

    ErrorDocument 404 /index.php
</IfModule> 

上述规则集在根 Codeigniter 应用程序中从 url 中删除 index.php 没有问题。但是这套规则似乎不允许public_html/land/.htaccess中的mod_rewrite规则执行。

当我删除 public_html/.htaccess 中的 mod_rewrite 规则时,public_html/land/.htaccess 中的 mod_rewrite 规则开始被评估。

有没有办法更改 public_html/.htaccess 中的 mod_rewrite 规则以处理旨在访问 /land/ 子文件夹的 url 的特殊情况?

我认为最好的解决方案可能是更改 public_html/.htaccess 中的 mod_rewrite 规则,以允许在 url 中处理子文件夹时执行 public_html/land/.htaccess 中的 mod_rewrite 规则。我愿意接受任何建议。

先发制人地回答“为什么不使用子域?”这个问题1. 在 SSL 证书上省钱。 2) 非技术用户有时会对营销基础域名的子域感到困惑。

先发制人地回答“为什么不将 Codeigniter 应用程序结合起来使用框架中的相同文件?”复制框架文件是保持版本存储库分离的一种简单方法。

【问题讨论】:

  • 将文件夹添加到 RewriteBase,如:RewriteBase /land/

标签: apache .htaccess mod-rewrite codeigniter-url


【解决方案1】:

如果它是请求字符串的一部分,则在顶部添加一条规则以仅转到land 子文件夹。这样,/land/.htaccess 中的规则将被执行,而不是 /.htaccess 中的后续规则。所以把它放在顶部:

RewriteRule ^land.*$ - [NC,L]

这将检查请求是否以“land”开头并将其重定向到子目录,其中将应用与该子目录对应的 .htaccess 规则。

现有规则检查文件和文件夹并且如果请求对应于其中之一则不进行重写的原因是因为请求中“land”后面的内容可能不是真实文件,因此会触发重写规则。

【讨论】:

  • 这对我不起作用。我在public_html/.htaccess 的规则顶部添加了这一行,但public_html/land/.htaccess 没有执行。我通过故意在public_html/land/.htaccess 中导致服务器错误进行测试,但.htaccess 文件永远不会运行并且没有服务器错误。只是一个 404 错误。
  • 404 错误很奇怪——它至少应该在土地子目录中找到要加载的内容,对吧?您可以添加规则来记录重写并签入日志吗? RewriteLog "/usr/local/apache/logs/rewrite.log" RewriteLogLevel 4 并确保日志文件夹和文件存在并且可以被网络服务器写入。
  • 这是记录重写的好建议。谢谢!
【解决方案2】:

问题是public_html/.htaccess 中的规则正在重写 URL 到 /land/,你需要一个直通来使它在请求 /land/ 时不会发生任何事情。添加:

RewriteRule ^land/ - [L]

在你的其他规则之前。

【讨论】:

  • 这对我不起作用。我在public_html/.htaccess 的规则顶部添加了这一行,但public_html/land/.htaccess 没有执行。我通过故意在public_html/land/.htaccess 中导致服务器错误进行测试,但.htaccess 文件永远不会运行并且没有服务器错误。只是一个 404 错误。
猜你喜欢
  • 2013-01-08
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2011-10-09
  • 2016-07-13
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多