php CodeIgniter htaccess 和 URL 重写问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14783666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter htaccess and URL rewrite issues
提问by Ty Bailey
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.
我以前从未使用过 CodeIgniter,更不用说任何 php 框架了,我想我会尝试一下。一切都很好,除了我似乎无法从 URL 中删除 index.php 并仍然访问我的页面。
I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.
我从未使用过 MVC 结构,所以我正在学习,所以如果我做错了,请原谅我。
I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/aboutbut currently I can only access it by using localhost/ci/index.php/about
我试图通过简单地输入localhost/ci/about来访问我创建的名为“about_page.php”的视图,但目前我只能使用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
页面的控制器是:/application/controllers/about.php页面
的模型是:/application/models/about_model.php页面
的视图是:/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 - 删除 index.php
Codeigniter - 如何从 url 中删除 index.php?
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/.htaccessand added this code to it:
CodeIgniter 在“应用程序”文件夹中带有一个 .htaccess 文件,其中只包含 .htaccess 文件Allow Deny From All。所以我在根目录中创建了一个新的 .htaccess 文件,http://localhost/ci/.htaccess并在其中添加了以下代码:
RewriteEngine On
RewriteBase /ci
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [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
当 .htaccess 文件位于根目录中时,我收到 500 Internal Server Error。当我将完全相同的文件复制到应用程序文件夹时,500 错误消失了,但我仍然无法使用localhost/ci/about访问 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.
我已经更改$config['index_page'] = 'index.php';为$config['index_page'] = '';并且我尝试更改$config['uri_protocol'] = 'AUTO';为,$config['uri_protocol'] = 'REQUEST_URI';但我仍然收到内部服务器错误。
I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.
我进入了 httpd.conf 文件并取消了 mod_rewrite.so 模块的注释,所以我知道 mod_rewrite 处于活动状态。
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.
有没有人知道为什么这不起作用或我如何获得这项工作?我知道在 StackOverflow 上有很多关于这个主题的问题,但我找不到一个能回答我的问题的问题。
Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/aboutor do I have to create an 'about' directory in the 'application' directory?
我这样做对吗?我是否应该能够通过访问localhost/ci/about来访问 about 页面,还是必须在“application”目录中创建一个“about”目录?
回答by Rahul Chipad
There are 3 steps to remove index.php.
删除index.php.有 3 个步骤。
Make below changes in
application/config.phpfile$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name'; $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO';Make
.htaccessfile in your root directory using below codeRewriteEngine on RewriteCond !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/ [L,QSA]Enable the rewrite engine (if not already enabled)
i.First, initiate it with the following command:
a2enmod rewriteii.Edit the file
/etc/apache2/sites-enabled/000-defaultChange all
AllowOverride NonetoAllowOverride All.Note: In latest version you need to change in
/etc/apache2/apache2.conffileiii.Restart your server with the following command:
sudo /etc/init.d/apache2 restart
在
application/config.php文件中进行以下更改$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name'; $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO';.htaccess使用以下代码在根目录中制作文件RewriteEngine on RewriteCond !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/ [L,QSA]启用重写引擎(如果尚未启用)
一世。首先,使用以下命令启动它:
a2enmod rewriteii. 编辑文件
/etc/apache2/sites-enabled/000-default全部更改
AllowOverride None为AllowOverride All.注意:在最新版本中,您需要更改
/etc/apache2/apache2.conf文件三、使用以下命令重新启动服务器:
sudo /etc/init.d/apache2 restart
回答by jmadsen
Your .htaccessis slightly off. Look at mine:
你.htaccess的有点不对劲。看看我的:
RewriteEngine On
RewriteBase /codeigniter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /codeigniter/index.php?/ [L]
Notice "codeigniter" in two places.
注意两个地方的“codeigniter”。
after that, in your config:
之后,在您的配置中:
base_url = "http://localhost/codeigniter"
index = ""
Change codeigniter to "ci" whereever appropriate
在适当的地方将 codeigniter 更改为“ci”
回答by Vinilnbr
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dmizone_bkp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /dmizone_bkp/index.php?/ [L]
</IfModule>
回答by William Kheng
This works for me
这对我有用
Move your .htaccessfile to root folder (locate before application folder in my case)
将您的.htaccess文件移动到根文件夹(在我的情况下位于应用程序文件夹之前)
RewriteEngine on
RewriteBase /yourProjectFolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$config['base_url'] = "";
$config['index_page'] = "index.php";
$config['uri_protocol'] = "AUTO";
[PT,L]
Mine config.php looks like this (application/config/config.php)
我的 config.php 看起来像这样(application/config/ config.php)
$config['uri_protocol'] = 'AUTO';
Let me know if its working for you guys too ! Cheers !
让我知道它是否也适合你们!干杯!
回答by Kusaasira Joshua
For those users of wamp server, follow the first 2 steps of @Raul Chipad's solution then:
对于 wamp 服务器的那些用户,请按照@Raul Chipad 解决方案的前 2 个步骤进行操作:
- Click the wamp icon (normally in green), go to "Apache" > "Apache modules" > "rewrite_module". The "rewrite_module" should be ticked! And then it's the way to go!
- 单击 wamp 图标(通常为绿色),转到“Apache”>“Apache 模块”>“rewrite_module”。应该勾选“rewrite_module”!然后就是要走的路!
回答by Velja Matic
I am using something like this - codeigniter-htaccess-file, its a good article to begin with.
我正在使用这样的东西 - codeigniter-htaccess-file,这是一篇很好的文章。
- leave the .htaccess file in CI root dir
- make sure that mod_rewrite is on
- check for typos (ie. controller file/class name)
- in /application/config/config.php set
$config['index_page'] = ""; - in /application/config/routes.php set yourdefault controller
$route['default_controller']="home";
- 将 .htaccess 文件保留在 CI 根目录中
- 确保 mod_rewrite 已打开
- 检查拼写错误(即控制器文件/类名)
- 在 /application/config/config.php 设置
$config['index_page'] = ""; - 在 /application/config/routes.php 中设置您的默认控制器
$route['default_controller']="home";
If you are running clean installation of CI (2.1.3) there isn't really much that could be wrong.
如果您正在运行 CI (2.1.3) 的全新安装,那么可能不会出错。
- 2 config files
- controller
- .htaccess
- mod_rewrite
- 2个配置文件
- 控制器
- .htaccess
- mod_rewrite
read
读
回答by Ahmed Abd ElHameed
if not working
如果不工作
$config['uri_protocol'] = 'PATH_INFO';
change it to
将其更改为
$config['uri_protocol'] = 'QUERY_STRING';
if not working change it to
如果不起作用,请将其更改为
$config['uri_protocol'] = 'ORIG_PATH_INFO';
if use this
如果使用这个
DirectoryIndex index.php
RewriteEngine on
RewriteCond !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/ [L,QSA]
with redirect or header location to url not in htaccess will not work you must add the url in htaccess to work
将重定向或标头位置重定向到不在 htaccess 中的 url 将不起作用,您必须在 htaccess 中添加该 url 才能工作
回答by ljgww
I had similar issue on Linux Ubuntu 14.
我在 Linux Ubuntu 14 上遇到了类似的问题。
After constant 403 error messages in the browser and reading all answers here I could not figure out what was wrong.
在浏览器中不断出现 403 错误消息并阅读此处的所有答案后,我无法弄清楚出了什么问题。
Then I have reached for apache's error log, and finally got a hint that was a bit unexpected but perfectly logical.
然后我找到了 apache 的错误日志,最后得到了一个有点出乎意料但完全合乎逻辑的提示。
the codeIgniter main folder had no X permission on folder rendering everything else unreadable by web server.
codeIgniter 主文件夹在文件夹上没有 X 权限,使 Web 服务器无法读取其他所有内容。
chmod ugo+x yourCodeIgniterFolder
chmod ugo+x yourCodeIgniterFolder
fixed first problem. Killed 403 errors.
解决了第一个问题。杀死了 403 错误。
But then I started getting error 500.
但后来我开始收到错误 500。
Culprit was a wrong settings line in .htaccess
罪魁祸首是 .htaccess 中的错误设置行
Then I started getting php errors.
然后我开始收到 php 错误。
Culprit was same problem as main folder no X flag for 'others' permission for inner folders in application and system folders.
罪魁祸首是与主文件夹相同的问题,对于应用程序和系统文件夹中的内部文件夹的“其他”权限没有 X 标志。
My two cents for this question: READ Apache Error.log.
我对这个问题的两分钱:阅读 Apache Error.log。
But, also be aware of security issues. CodeIgniter suggests moving application and system folder out of web space therefore changing permissions shall be taken with a great care if these folders remain in web space.
但是,也要注意安全问题。CodeIgniter 建议将应用程序和系统文件夹移出 Web 空间,因此如果这些文件夹保留在 Web 空间中,则更改权限时应格外小心。
Another thing worth mentioning here is that I have unzipped downloaded CodeIgniter archive directly to the web space. Folder permissions are likely created straight from the archive. As given they are secure, but folder access issue (on unix like systems) is not mentioned in CodeIgniter manual https://codeigniter.com/user_guide/installation/index.htmland perhaps it shall be mentioned there with guidelines on how to relax security for CodeIgniter to work while keeping security tight for the production system (some security hints are already there in the manual as mentioned beforehand).
这里值得一提的另一件事是,我已将下载的 CodeIgniter 存档直接解压缩到 Web 空间。文件夹权限可能直接从存档中创建。既然它们是安全的,但是 CodeIgniter 手册https://codeigniter.com/user_guide/installation/index.html 中没有提到文件夹访问问题(在类似 unix 的系统上),也许应该在那里提到有关如何放松的指南CodeIgniter 工作的安全性,同时保持生产系统的安全性(如前所述,手册中已经存在一些安全提示)。
回答by Kiran Maniya
Open the application/config/config.phpfile and make the changes given below,
打开application/config/config.php文件并进行以下更改,
set your base url by replacing the value of
$config['base_url'], as$config['base_url'] = 'http://localhost/YOUR_PROJECT_DIR_NAME';make the
$config['index_page']configuration to empty as$config['index_page'] = '';
通过替换 的值来设置您的基本网址
$config['base_url'],如$config['base_url'] = 'http://localhost/YOUR_PROJECT_DIR_NAME';使
$config['index_page']配置为空$config['index_page'] = '';
Create new .htaccessfile in project root folder and use the given settings,
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|images|js|css|uploads|favicon.png|favicon.ico|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
在项目根文件夹中创建新的.htaccess文件并使用给定的设置,
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|images|js|css|uploads|favicon.png|favicon.ico|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Restart the server, open the project and you'll be good to go.
重新启动服务器,打开项目,你就可以开始了。
回答by Siddharth Shukla
Just add this in the .htaccess file:
只需在 .htaccess 文件中添加:
##代码##
