Codeigniter没有路由权 [英] Codeigniter isn't routing right

查看:112
本文介绍了Codeigniter没有路由权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了Apache for Windows。我买了CodeIgniter Professional并下载了他们的源代码。它说,我应该把它的.htaccess在网站的根文件夹,所以我没有。我将基本网址设置为 http://127.0.0.1/kids/ 其中kids是根文件夹网站。它显示主页很好。当我点击一个链接,它总是给出404错误。它说,在此服务器上找不到请求的URL /index.php/welcome/cat/2。我尝试了几个可能的.htaccess在目录没有成功。在.htaccess中,它指示:

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

解释为什么它不工作?我也检查了configs.php和它似乎确定以及。 config ['index_page']设置为空白。我删除了.htaccess文件,仍然有同样的问题,除了它说:在这个服务器上找不到请求的URL / kids / welcome / cat / 2。



时间浪费在尝试让它工作,我失去了耐心。



这里是来自CodeIgniter Professional的说明:



要使这些文件夹可以访问CodeIgniter,在您的网站的根文件夹中创建一个小的.htaccess文件这里是什么.htaccess文件将是什么样子。它基本上包含指令allmvs某些文件和文件夹可以在网站上查看:

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

你熟悉mod_rewrite在Apache,你会注意到这个规则表面上删除index.php从任何目的地的URL。下面当你配置CodeIgniter(特别是config.php文件),你会看到为什么这是一个好的主意如果你不熟悉Apache中的mod_rewrite,不要太怕这个文件。基本上,.htaccess文件规定了几个规则,其中一个从所有的URL中删除index.php。文件中的其他规则允许使用您将来需要的各种其他文件夹(例如包含CSS,JavaScript,Captcha文件和图像的文件夹)。



我做的只是按照指示...把htaccess文件在根文件夹中,但是,它给出404错误。如果我从根文件夹中删除htaccess文件,它显示主页如果我添加htaccess文件,它给出404错误显然,Apache理解了htaccess,否则它不会显示404错误。

解决方案

您的路线可能无法正确解析,但我不确定,建议您采取以下步骤。



首先,只要取消使用 PATH_INFO 为您的CodeIgniter路由,因为它不是必要的。将 .htaccess 文件更改为:

  RewriteEngine On 

RewriteCond $ 0!^(index\.php | images | captcha | css | js | robots\.txt)
RewriteRule ^。* $ index.php [L]


$ b b

然后,在您的配置中,确保 $ config ['uri_protocol'] 设置为 AUTO (默认,我相信)或 REQUEST_URI



之后,最可能的原因是, URL上有一个尾部斜杠。我不知道CodeIgniter知道配置的特定设置,但是你可以导致你的URL有一个尾部斜线与以下配置指令:

  $ config ['url_suffix'] =/; 

编辑:确保 mod_rewrite 正在工作,以及。在 RewriteEngine 指令之后,在 .htaccess 文件的顶部尝试此操作:

  RewriteRule。* http://stackoverflow.com/ [L,R] 

如果你被重定向到Stack Overflow,至少我们知道这是工作。如果没有,你没有得到500错误,所以问题将在 .htaccess 文件,在这种情况下你应该确认相应的<$ c $在您的服务器配置中您的网站位于磁盘上的 c> c>条目已经 AllowOverride All
$ b

编辑:我应该更好地了解正斜杠,对不起。出现此问题是因为 mod_rewrite 在您的每个目录( .htaccess )上下文中有效。



一旦你的URL被重写,它会被检查以确定 mod_rewrite 应该做什么。当您的URL指向本地资源并包含前导斜杠时, mod_rewrite 保持原样。



当它保持原样时,内部重定向到 /index.php ,在这种情况下,假定为来自主机(localhost)的完全限定URL。因此,请求的资源最终为 http://localhost/index.php 。当添加per-directory前缀时,重写引擎中的后续步骤将尝试从路径中删除 DocumentRoot (如果存在)。这将留给我们 /kids/index.php ,然后传递到内部重定向,并按预期解决。


I installed Apache for Windows. I bought CodeIgniter Professional and downloaded their source code. It said that I should put its .htaccess in the root folder of the website so I did. I set base URL to be http://127.0.0.1/kids/ where kids is the root folder of the website. It showed the homepage just fine. When I clicked on a link, it always gives the 404 error. It said, "The requested URL /index.php/welcome/cat/2 was not found on this server." I tried several possiblities of .htaccess in directories with no success. Inside the .htaccess, it instructs:

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

Explain why it's not working at all? I also checked configs.php and it seems ok as well. config['index_page'] is set to blank. I removed the .htaccess file and still same problem except that it said, "The requested URL /kids/welcome/cat/2 was not found on this server."

Hours have been wasted on trying to get it working and I am losing patience.

Here's the instruction from CodeIgniter Professional:

"To make those folders accessible to CodeIgniter, you will also need to create a small .htaccess file in the root folder of your web site. Here's what that .htaccess file would look like. It basically contains instructions that allmvs certain files and folders to be viewable on the web site:

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

If you are familiar with mod_rewrite in Apache, you will note that this rule ostensibly removes index.php from the URL of any destination. Below when you're configuring CodeIgniter (and particularly the config.php file), you'll see why this is a good idea. If you're not familiar with mod_rewrite in Apache, don't be too intimidated by this file. Basically, the .htaccess file sets forth a few rules, one of which removes index.php from all URLs. Other rules in the file allow the use of various other folders that you will need going forward (such as folders that contain CSS, JavaScript, Captcha files, and images)."

I did just as instructed... put the htaccess file in root folder and yet, it gave 404 error. If I remove htaccess file from the root folder, it shows homepage. If I add htaccess file, it gives 404 error. I moved htaccess and it didn't do anything to re-route. Obviously, the Apache understood the htaccess otherwise it wouldn't show the 404 error.

解决方案

It's possible that your routes aren't resolving correctly, but I'm not sure. I'd recommend the following steps.

First, I would just do away with using PATH_INFO for your CodeIgniter routing, as it's not necessary. Change your .htaccess file to this:

RewriteEngine On

RewriteCond $0 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^.*$ index.php [L]

Then, in your configuration, make sure that $config['uri_protocol'] is set to AUTO (the default, I believe) or REQUEST_URI.

Following that, the most likely cause is that your routes expect there to be a trailing slash on the URL. I don't know enough about CodeIgniter to know where that particular setting is configured, but you can cause your URLs to have a trailing slash with the following configuration directive:

$config['url_suffix'] = "/";

Edit: Make sure that mod_rewrite is working, as well. Try this at the top of your .htaccess file, after the RewriteEngine directive:

RewriteRule .* http://stackoverflow.com/ [L,R]

If you get redirected to Stack Overflow, at least we'll know that's working. If not, you aren't getting a 500 error, so the problem would lie with the .htaccess file, and in that case you should confirm that the corresponding <Directory> entry in your server configuration for where your site is located on disk has AllowOverride All set.

Edit: I should have known better about the forward slash, sorry about that. The problem occurs because of how mod_rewrite works in your per-directory (.htaccess) context.

Once your URL is rewritten, it is examined to determine what mod_rewrite should do with it. When your URL points to a local resource and contains a leading slash, mod_rewrite leaves it as-is. When it does not, it adds the per-directory prefix.

When it's left as-is, an internal redirect to /index.php, in this case, is assumed to be a fully-qualified URL from the host (localhost). So, the resource requested ends up being http://localhost/index.php. When the per-directory prefix is added, a later step in the rewrite engine attempts to remove the DocumentRoot from the path, if it's present. This leaves us with /kids/index.php, which is then passed to the internal redirect, and is resolved as expected.

这篇关于Codeigniter没有路由权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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