.htaccess - 如何编写.htaccess文件以使CodeIgniter URL路由正常工作?

标签 .htaccess codeigniter

我正在使用CodeIgniter运行LAMP环境。我希望能够使用它的URL模式,例如http://localhost/controller/function/ID,但是默认情况下它必须是http://localhost/index.php/controller/function/ID。用户指南说,我需要一个.htaccess文件来使前一种样式成为可能,并以此为例:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

但是我不明白这是如何工作的。我应该把.htaccess放在哪里?它必须在服务器根目录中吗?

如果我的服务器上运行多个站点怎么办。是否必须将其放在/var/www/中并根据加载的目录指定条件?或者我可以将它放在/var/www/site_using_codeigniter/中吗?我不确定如何使它正常工作。

我能否做到这一点,使其仅在所请求的URL实际上不存在时才映射到MVC URL?例如,http://localhost/articles/2009/some-article-name不会是服务器上的目录,因此它将映射到index.php并提供给codeigniter,但是http://localhost/forum将存在于服务器上,但不应由codeigniter处理。

我应该对.htaccess文件做些其他事情吗?有什么好读的关于他们的吗?

更新

我这样更改了配置文件:
$config['index_page'] = ""; //used to be "index.php"

并将其添加到.htaccess文件中:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

并将其保存在我的网站根目录中(在本例中为/var/www/cms/)

我进入了/etc/apache2/conf.d/security(包含在/etc/apache2/apache2.conf中)并执行以下操作:
<Directory />
    AllowOverride All #was "none", but this was all commented out
    Order Deny,Allow
    Deny from all
</Directory>

但是,它仍然无法正常工作。如果导航到http://localhost/cms/index.php/hello,它会显示“Hello there”,但http://localhost/cms/hello会显示404错误。

有想法吗?

更新:非常成功!

我必须仔细研究我的apache配置文件,以找到所有目录的定义位置(在此之前,我从未在服务器配置文件中搞乱过),并且发现讨厌的AllowOverride None阻碍了我的卓越计划。

现在,它可以完美运行了。这里的所有答案都是可行且可行的。

最佳答案

在您的system/application/config/config.php中,更改

$config['index_page'] = "index.php";


$config['index_page'] = "";

并在.htaccess中添加以下内容:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|scripts|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

将您的.htaccess文件添加到system文件夹所在的目录中,因此在CodeIgniter根目录中,您应该
  • 系统/
  • user_guide /
  • index.php
  • .htaccess
  • license.txt

  • 另外,由于您的服务器上运行着多个站点,因此您可能需要虚拟主机。将其添加到您拥有的每个站点的apache2.conf的最后一部分:
    Listen *:11000
    <VirtualHost *:11000>
         ServerAdmin you@somewhere.com
         DocumentRoot "/var/www/cms"
         ServerName you-dummy.com
         <Directory "/var/www/cms">
              AllowOverride All
              Options +Indexes
              DirectoryIndex index.php
              Order allow,deny
              Allow from all
         </Directory>
         ErrorLog logs/cms_log
         CustomLog logs/cms_log common
    </VirtualHost>
    

    您现在可以从http://localhost:11000/访问站点,并使用http://localhost:11000/hello访问 Controller 。

    关于.htaccess - 如何编写.htaccess文件以使CodeIgniter URL路由正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1397014/

    相关文章:

    CodeIgniter:使用 Tank_auth 将字段传递到 user_profiles 数据库

    php - Codeigniter echo/var_dump 不工作

    php - ajax 表单请求不适用于 mod_rewrite

    php - 使用 htaccess 拒绝 ajax 文件访问

    php - .htaccess 将子域重写到目录并将子域保留在 url 中

    .htaccess - 在共享托管服务器上使用 codeigniter 感到困惑

    .htaccess - 使用 gulp 复制 .​​htaccess(一个点文件)失败

    file - codeigniter 文件上传 - 可选?

    css - 在 MVC 架构中存储网站的样式表

    codeigniter - CodeIgniter 页面方向中的 mPDF 不会横向显示