如何将myapp / index.php / controller / view映射到myapp / controller / view?

时间:2010-07-17 02:31:41

标签: php apache

我正在使用PHP的CodeIgniter框架。它要求index.php应该在我键入的任何URL中。我该如何防止这种情况?任何人都可以向我提供一个简单的.htaccess文件,它会隐式将其映射到index.php,因此用户不必每次都输入它吗?

提前致谢:)

2 个答案:

答案 0 :(得分:1)

您可以使用.htaccess文件(或更好的apache.conf)将所有请求转发到index.php并使用mod_rewrite:

Options +FollowSymLinks
IndexIgnore */*

<ifmodule mod_rewrite.c> 
RewriteEngine on

    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php
    RewriteRule . index.php
</ifmodule>

如果您的请求是http://www.example.com/controller/view,则$ _SERVER ['REQUEST_URI']是/ controller / view。您可以使用Regex或简单地拆分字符串来解释它:explode('/',$ _ SERVER ['REQUEST_URI']);

答案 1 :(得分:1)

你需要读这个 http://codeigniter.com/wiki/mod_rewrite/ 因为你还需要在Svens的回答中做一个小代码更改(在步骤2中)。