htaccess的重写所有的请求到一个控制器 [英] htaccess rewriting all requests to a single controller

查看:128
本文介绍了htaccess的重写所有的请求到一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以类似的方式codeigniter我想捕获所有请求,并将其重写到index.php文件在我的web访问的文件夹的根目录下哪些网站。不同于在codeigniter的例子​​,但是我不想来检查文件是否存在之前,我把它改写(因为我不希望允许直接访问任何文件)。

我不得不使用下面的重写规则部分成功:

 重写规则^(。*)$的index.php / $ 1 / [L]
 

不过,我可以让它开始工作的唯一方法是通过添加重写条件之前,做一些种类的检查,以确保该请求不适合的index.php,否则我得到一个500内部服务器错误。 Ive有看看错误日志,似乎是由于过于reccursion。我明白,如果它的index.php请求犯规反正无所谓,因为他们将达到正确的文件,但我不明白,需要对这种情况,我觉得脏包括它时,我不知道为什么它在那里?

工作规则......

 的RewriteCond%{REQUEST_URI}!^ / index.php文件/
 

其还值得一提的是,重写条件的工作藏汉但同样我不知道为什么它的需要!

 的RewriteCond%{} REQUEST_FILENAME!index.php文件
 

解决方案

的RewriteCond意味着,如果这些条件得到满足以下重写规则仅被评估。 在你的情况下,在写着如果请求的URI的的开始的index.php,然后重新路由到index.php。

您需要的检查,否则你会被重新路由的index.php到自身无限循环。

In a similar way to codeigniter I want to catch all requests and rewrite them to an index.php file which sites in the root of my web accessible folder. Unlike the examples on codeigniter however I do not want to check whether the file exists before I rewrite it (because I dont want to allow direct access to any files).

I have had partial success using the following rewrite rule:

RewriteRule ^(.*)$ index.php/$1/ [L]

However the only way I can get it to work is by adding a rewrite condition before it which does some sort of check to make sure the request is not for index.php, otherwise I get a 500 internal server error. Ive had a look at the error log and it seems it is due to too much reccursion. I understand that if the request index.php it doesnt matter anyway because they will hit the right file but I dont understand the need for this condition and I feel dirty including it when I dont know why it has to be there?

The working rule is...

RewriteCond %{REQUEST_URI} !^/index.php/

Its also worth mentioning that this rewrite condition worked aswell but again I dont know why its needed!

RewriteCond %{REQUEST_FILENAME} !index.php

解决方案

RewriteCond means, the following RewriteRule is only evaluated if these conditions are met. In your case, in words "If the requested URI doesn't start with /index.php, then reroute to index.php".

You need that check, because otherwise you would be rerouting index.php to itself in an infinite loop.

这篇关于htaccess的重写所有的请求到一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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