繁体   English   中英

Codeigniter的URL重写

[英]Codeigniter's 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]

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

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

我现在能做什么?

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

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,此版本稍作修改。

这是我用于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> 

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

将此代码段放在项目根目录下的.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";
# 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]

我使用它,它完美地工作!

将以下内容插入.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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM