使用htaccess从CodeIgniter中的URL中删除子目录

时间:2013-08-16 07:45:29

标签: .htaccess codeigniter url-rewriting url-routing codeigniter-url

我的CodeIgniter项目放在子文件夹中,我想用.htaccess隐藏这两个子文件夹。

一切正常,但我目前的网址如下:

example.com/folder1/folder2/ci_project/class/function/$id

我希望这是:

example.com/class/function/$id

现在,我使用这个简单的.htaccess代码从网址中删除index.php

Options +FollowSymLinks
RewriteEngine on

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

任何想法?

当我在folder1 / folder2 / ci_project里面的htaccess文件中使用以下代码时,它会隐藏子目录,但是我收到错误“找不到对象”

RewriteCond%{REQUEST_URI}!^ / folder1 / folder2 / ci_project /

RewriteRule ^(。*)$ / folder1/folder2/ci_project/index.php/$1 [L]

1 个答案:

答案 0 :(得分:0)

将您当前的.htaccess文件从ci_project/移至域根目录,并在其中替换这些代码:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on

    RewriteCond $1 !^(robots\.txt|favicon\.ico|public)
    RewriteRule ^(.*)$ /folder1/folder2/ci_project/index.php/$1 [L]
</IfModule>

在这种情况下,您应将公共文件放在域名根目录下的public文件夹中。

如果您不希望public对其他文件夹进行处理,则应将其他文件夹(例如RewriteCond)添加到RewriteRule

如果同一域中存在多个应用程序,您可能需要考虑以下选项:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on

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