【问题标题】:Using htaccess to remove sub-directories from URL in CodeIgniter在 CodeIgniter 中使用 htaccess 从 URL 中删除子目录
【发布时间】:2013-08-18 13:34:41
【问题描述】:

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

一切正常,但我当前的 URL 如下所示:

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

我希望这是:

example.com/class/function/$id

现在,我正在使用这个简单的 .htaccess 代码来从 URL 中删除 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/

重写规则 ^(.*)$ /folder1/folder2/ci_project/index.php/$1 [L]

【问题讨论】:

  • 您可以发布您当前使用的 .htaccess 吗?
  • 将 index.php 文件放在根目录外,并将其中的应用程序和系统路径设置为子文件夹。
  • 任何反馈将不胜感激。您可以尝试一下当前的答案吗?

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


【解决方案1】:

将您当前的.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

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

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

<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>

【讨论】:

    猜你喜欢
    • 2012-06-25
    • 2021-06-24
    • 2013-05-18
    • 2020-05-11
    • 2011-10-09
    • 2013-05-28
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多