mod_rewrite从子目录中的Codeigniter中删除index.php

时间:2012-05-10 18:59:28

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

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/中。因此,我需要在寻址根index.php文件夹的网址中排除字符串/,并在index.php子文件夹中排除字符串/land/

根文件夹中的.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/子文件夹的网址的特殊情况?

我认为最好的解决方案可能是更改public_html/.htaccess中的mod_rewrite规则,以便在URL中寻址子文件夹时允许public_html/land/.htaccess中的mod_rewrite规则执行。我对任何建议持开放态度。

对问题“你为什么不使用子域名?”的先发制人的回答? 1.在SSL证书上省钱。 2)非技术用户有时会被子域名混淆,以便营销基础域名。

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

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:1)

问题是public_html/.htaccess中的规则正在重写要转到/land/的网址,您需要一个直通,这样才能在请求/land/时没有任何反应。添加:

RewriteRule ^land/ - [L]

在你的其他规则之前。

相关问题