CodeIgniter htaccess和URL重写问题 [英] CodeIgniter htaccess and URL rewrite issues
问题描述
我从来没有使用过CodeIgniter,更不用说任何PHP框架,我想我会试试。一切都很好,除了我似乎无法从URL中删除index.php,仍然访问我的网页。
我从来没有使用MVC结构,所以我在学习,我原谅,如果我做错了,请原谅我。
我想通过输入 localhost / ci / about 访问我创建的名为'about_page.php'的视图,但目前我只能使用 localhost /ci/index.php/about
此页面的控制器为: /application/controllers/about.php br />
该页面的模型是: /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在应用程序中提供了一个.htaccess文件'文件夹,其中只包含允许拒绝所有
。所以我在根目录中创建了一个新的.htaccess文件, http://localhost/ci/.htaccess
并添加了这个代码:
RewriteEngine On
RewriteBase / ci
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^(。*)$ index.php / $ 1 [L]
.htaccess文件是在根目录我得到一个500内部服务器错误。当我将完全相同的文件复制到应用程序文件夹中时,500错误消失,但仍然无法使用 localhost / ci / about
$ b $访问about页面b
我已经更改 $ config ['index_page'] ='index.php'; 到
$ config ['index_page'] = '$
$
$
$
$
uri_protocol'] ='REQUEST_URI';
但我仍然得到内部服务器错误。
我进入httpd.conf文件并取消注释mod_rewrite.so模块,所以我知道mod_rewrite是活跃的。
有没有人有任何想法为什么这不工作或我该如何得到这项工作?我知道在这个问题上有很多关于StackOverflow的问题,但我找不到一个回答我的问题。
我这样做吗?我是否应该可以访问 localhost / ci / about 访问关于页面,或者我必须在应用程序目录中创建一个关于目录吗?
有3个步骤来删除 index.php
。
-
在
application / config.php
文件中进行以下更改$ config ['base_url'] ='http://'.$_SERVER ['SERVER_NAME']。'/您的Ci folder_name';
$ config ['index_page'] ='';
$ config ['uri_protocol'] ='AUTO';
-
将
.htaccess
您的根目录使用以下代码RewriteEngine on
RewriteCond $ 1!^(index\.php | resources | \.txt)
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^(。*)$ index.php / $ 1 [L,QSA]
-
启用重写引擎(如果尚未启用)
i。首先,使用以下命令启动它:
a2enmod重写
ii。编辑档案
/ etc / apache2 / sites-enabled / 000-default
全部更改
AllowOverride None
AllowOverride All
。
注意:在最新版本中,您需要更改
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
.
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';
Make
.htaccess
file in your root directory using below codeRewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA]
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
toAllowOverride All
.Note: In latest version you need to change in
/etc/apache2/apache2.conf
fileiii. Restart your server with the following command:
sudo /etc/init.d/apache2 restart
这篇关于CodeIgniter htaccess和URL重写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!