【问题标题】:How to redirect index.php with CodeIgniter being a subpage如何以 CodeIgniter 为子页面重定向 index.php
【发布时间】:2014-01-07 15:27:50
【问题描述】:

我是 CodeIgniter 的新手,而 index.php 实在是太麻烦了。

我想要一些友好的 URL,但我的网站位于子文件夹中。

我安装了干净的 CodeIgniter 安装,添加了另一个视图用于测试目的,这就是我得到的。

Page 1
http://www.example.com/webadmintest/
http://www.example.com/webadmintest/index.php/welcome/index

Page 2
http://www.example.com/webadmintest/index.php/welcome/byebye

这很完美。

现在在配置文件中我改为:

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

并在应用文件夹的.htacces中添加了这个(不知道是这个地方还是根CodeIgniter文件夹)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

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

我仍然可以加载以前的链接,但如果我尝试访问这些链接,它会将我转到 www.example.com 主页面。

 Page 1
    http://www.example.com/webadmintest/
    http://www.example.com/webadmintest/welcome/index

 Page 2
 http://www.example.com/webadmintest/welcome/byebye

我也尝试过使用:

$config['base_url'] = '/webadmin/';

RewriteBase /webadmintest/

RewriteRule ^(.*)$ /webadmintest/index.php?/$1 [L]

如您所见,这不是典型的问题,我的意思是,如果这是一个独立的网站,我可能不会遇到这个问题,但要求网站是主网站的子文件夹。

谢谢

【问题讨论】:

  • 您想删除文件 index.php 还是只想将其从 URL 中删除?
  • 我希望将其从 URL 中删除
  • 我已经尝试了 kumar_v 的答案中的一段代码,并且在某种程度上它正在工作。我的主页通过这两个链接加载 www.example.com/backend/ www.example.com/backend/main/ 但是第二页没有加载 www.example.com/backend/clients www.example.com/backend/clients/index 它曾经加载为www.example.com/backend/index.php/clients/index

标签: .htaccess codeigniter redirect


【解决方案1】:

我猜你有 mod_rewrite,如果不是 Get mod_rewrite here

在app文件夹>config文件夹>config.php

$config['index_page'] = 'index.php'; -> $config['index_page'] = '';

【讨论】:

  • 我照做了,还是不行。
  • 我将 CI 安装在一个新文件夹中并从头开始制作,但什么也没发生。
【解决方案2】:

终于找到答案了。

这适用于在子文件夹中拥有 CodeIgniter 的每个人

首先是\subfolder\application\config\config.php

你必须找到这些行并用这些来改变它:

$config['base_url'] = 'http://www.example.com/subfolder/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

接下来你必须修改你的 \subdfolder\application.htaccess :

Options -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /subfolder/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

    #When your application folder isn't in the system folder

    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>
    ErrorDocument 404 /index.php
</IfModule>

然后在 \subfolder.htaccess 中创建一个新的 .htaccess 并添加以下代码:

RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L]

感谢@saurabh2836 和@Robin Morgan

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-24
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2012-06-07
    • 1970-01-01
    • 2016-06-02
    相关资源
    最近更新 更多