Codeigniter的URL重写

时间:2010-06-08 04:59:56

标签: php .htaccess codeigniter url url-rewriting

我正在使用以下htaccess脚本,以便我可以隐藏URI中的index.php

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

但我面临一个重大问题:(

我的assets文件旁边有一个名为index.php的目录,它应该在那里。当我通过浏览器浏览目录时,会显示Codeigniter的未找到页面。我无法浏览文件/assets/image.jpg,但是当我从<img>代码中调用它时会显示该文件

我现在能做什么?

请注意,它在我的本地服务器(localhost)中工作,但不在实时服务器中。

5 个答案:

答案 0 :(得分:7)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [NC,L]

这将传递Apache不会向CodeIgniter提供服务的任何请求。这样您就可以自由地创建目录而无需更新重写规则,并且您无需在Apache中设置404处理(甚至在图像/资源目录中)。

Zend Framework的作者在其用户指南中推荐了这一点。对于CodeIgniter,此版本略有修改。

答案 1 :(得分:0)

这是我用于CI安装的.htaccess文件:

如果您在子文件夹中有它,则需要编辑RewriteBase。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

答案 2 :(得分:0)

这是我正在使用的完整解决方案,并且发现效果很好:

将此代码段放在项目根目录下的.htaccess文件中,如http://localhost/myProject

RewriteEngine On
RewriteBase /appDirName
RewriteCond %{REQUEST_URI} ^appDirName.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

位于system \ application \ config \ config.php

的CI配置文件中
$config['uri_protocol'] = "REQUEST_URI";

答案 3 :(得分:0)

# Turn on URL rewriting
RewriteEngine On
# Put your installation directory here:
RewriteBase /
# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the images/, js/, or css/ directories 
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|images|js|css)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

我使用它并且它完美地工作!

答案 4 :(得分:0)

将以下内容插入.htaccess

# Change "welcome" to your default controller:
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/$ $1 [L,R=301]

# Checks to see if the user is attempting to access a valid file, 
# if not send them to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]