CodeIgniter htaccess 和 URL 重写问题 [英] CodeIgniter htaccess and URL rewrite issues

查看:23
本文介绍了CodeIgniter htaccess 和 URL 重写问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用过 CodeIgniter,更不用说任何 php 框架了,我想我会尝试一下.一切都很好,除了我似乎无法从 URL 中删除 index.php 并仍然访问我的页面.

我从未使用过 MVC 结构,所以我边学边做,所以如果我做错了,请原谅我.

我正在尝试通过简单地输入 localhost/ci/about 来访问我创建的名为about_page.php"的视图,但目前我只能使用 localhost/ci/访问它index.php/about

页面的控制器是:/application/controllers/about.php
页面的模型是:/application/models/about_model.php
页面的视图是:/application/views/about_page.php

我已经搜索了此问题的解决方案,但一直没有找到.这是我已经搜索过的地方:

CodeIgniter - 删除 index.php
Codeigniter - 如何从 url 中删除 index.php?
http://www.farinspace.com/codeigniter-htaccess-file/

CodeIgniter 在application"文件夹中带有一个 .htaccess 文件,该文件仅包含 Allow Deny From All.所以我在根目录下创建了一个新的 .htaccess 文件,http://localhost/ci/.htaccess 并添加了这段代码:

RewriteEngine On重写基/ciRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^(.*)$ index.php/$1 [L]

当 .htaccess 文件位于根目录中时,我收到 500 内部服务器错误.当我将完全相同的文件复制到应用程序文件夹时,500 错误消失了,但我仍然无法使用 localhost/ci/about

访问关于页面

我已经将 $config['index_page'] = 'index.php'; 更改为 $config['index_page'] = ''; 并且我试过了将 $config['uri_protocol'] = 'AUTO'; 更改为 $config['uri_protocol'] = 'REQUEST_URI'; 但我仍然收到内部服务器错误.

我进入了 httpd.conf 文件并取消了 mod_rewrite.so 模块的注释,所以我知道 mod_rewrite 处于活动状态.

有没有人知道为什么这不起作用或我如何获得这项工作?我知道在 StackOverflow 上有很多关于这个主题的问题,但我找不到一个能回答我的问题的问题.

我这样做对吗?我是否应该能够通过访问 localhost/ci/about 来访问 about 页面,还是必须在application"目录中创建一个about"目录?

解决方案

删除index.php有3个步骤.

  1. application/config.php 文件中进行以下更改

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';$config['index_page'] = '';$config['uri_protocol'] = 'AUTO';

  2. 使用以下代码在根目录中制作 .htaccess 文件

    RewriteEngine onRewriteCond $1 !^(index.php|resources|robots.txt)RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则 ^(.*)$ index.php/$1 [L,QSA]

  3. 启用重写引擎(如果尚未启用)

    i. 首先,使用以下命令启动它:

    a2enmod 重写

    ii. 编辑文件 /etc/apache2/sites-enabled/000-default

    将所有 AllowOverride None 更改为 AllowOverride All.

    注意:最新版本需要在/etc/apache2/apache2.conf文件中修改

    iii. 使用以下命令重新启动服务器:

    sudo/etc/init.d/apache2 restart

I have never used CodeIgniter before, let alone ANY php framework and I thought I would give it a try. Everything is going fine except I cannot seem to remove the index.php from the URL and still access my pages.

I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.

I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about

The controller for the page is: /application/controllers/about.php
The Model for the page is: /application/models/about_model.php
And the View for the page is: /application/views/about_page.php

I have searched for a solution to this issue, but haven't been able to find one. Here is where I have already searched:

CodeIgniter - removing index.php
Codeigniter - how to remove the index.php from url?
http://www.farinspace.com/codeigniter-htaccess-file/

CodeIgniter comes with a .htaccess file in the 'application' folder which contains only Allow Deny From All. So I created a new .htaccess file in the root directory, http://localhost/ci/.htaccess and added this code to it:

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

When the .htaccess file is in the root directory I get a 500 Internal Server Error. When I copy the exact same file into the applications folder the 500 error goes away, but I still cannot access the about page by using localhost/ci/about

I have already changed $config['index_page'] = 'index.php'; to $config['index_page'] = ''; AND I tried changing $config['uri_protocol'] = 'AUTO'; to $config['uri_protocol'] = 'REQUEST_URI'; but I am still getting the Internal Server Error.

I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.

Does anyone have any ideas why this isn't working or how I can get this work? I know there are alot of questions on StackOverflow on this subject but I couldn't find one that answered my question.

Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/about or do I have to create an 'about' directory in the 'application' directory?

解决方案

There are 3 steps to remove index.php.

  1. Make below changes in application/config.php file

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    

  2. Make .htaccess file in your root directory using below code

    RewriteEngine on
    RewriteCond $1 !^(index.php|resources|robots.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    

  3. Enable the rewrite engine (if not already enabled)

    i. First, initiate it with the following command:

    a2enmod rewrite
    

    ii. Edit the file /etc/apache2/sites-enabled/000-default

    Change all AllowOverride None to AllowOverride All.

    Note: In latest version you need to change in /etc/apache2/apache2.conf file

    iii. Restart your server with the following command:

    sudo /etc/init.d/apache2 restart
    

这篇关于CodeIgniter htaccess 和 URL 重写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆