从ONl CI子目录的url中删除index.php

时间:2013-04-03 16:12:34

标签: .htaccess codeigniter rewrite

我了解如何从CI网址中删除index.php。以下工作正常:

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

但是,它会干扰现场网站上的其他代码。我正在添加的这个新CI版本位于名为v2的子目录中。因此布局就像:

-originalDir
  --v2
-otherDirs
-.htaccess
-etc...

所以从本质上讲,网址就像是:

// Original page
https://www.site.com/originalDir/somepage.htm
// And this exist too
https://www.site.com/originalDir/index.php

// My new stuff
https://www.site.com/originalDir/v2/index.php/controller/etc...
// Desired effect withOUT affecting the original sites index.php page
//     aka, the below is what i WANT but NOT getting!
https://www.site.com/originalDir/v2/controller/etc...

我可以用很多语言编写代码,但是从来没有很多这些htaccess重写的经验。关于如何使它重写index.php只有codeigniter子目录的任何想法?我尝试了一些似乎在本地工作的东西,但不是在实时服务器上。不确定重写代码的确切结构。

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用RewriteBase?

RewriteEngine On
RewriteBase /crm/v2
RewriteRule ^(.+)$ index.php?/$1 [L]

我没有专门用您的信息对此进行测试,但这是来自我用于某些项目的模板。

以下是RewriteBase的文档:http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase

我希望这有帮助!

答案 1 :(得分:1)

假设您只想重写CI目录中的规则,而不是将该目录之外的任何URL指向它......

将您的RewriteRule更改为仅匹配以CI目录开头的URI。

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

或者,您可以将.htaccess文件放在/crm/v2目录中,并指定RewriteBase

RewriteEngine on
RewriteBase /crm/v2/

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