简体   繁体   English

Codeigniter的URL重写

[英]Codeigniter's URL Rewriting

I'm using the following htaccess script so that I can hide index.php from the URI. 我正在使用以下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]

But I'm facing a major problem :( 但我面临一个重大问题:(

I have a directory named assets beside my index.php file and it should be there. 我的index.php文件旁边有一个名为assets的目录,它应该在那里。 When I browse the directory by browser, then Codeigniter's not found page displays. 当我通过浏览器浏览目录时,将显示Codeigniter 未找到的页面。 I can't browse the file /assets/image.jpg but it displays when I call it from an <img> tag 我无法浏览文件/assets/image.jpg但是当我从<img>标签调用它时它会显示

What can I do now? 我现在能做什么?

Note that it is working in my local server ( localhost ) but not in the live server. 请注意,它在我的本地服务器( 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]

That'll pass any request that Apache wouldn't serve off to CodeIgniter. 这将传递Apache不会向CodeIgniter提供服务的任何请求。 This way you're free to create directories all over without updating your rewrite rules, and you don't need to setup 404 handling in Apache (even inside your image/resource directories). 这样您就可以自由地创建目录而无需更新重写规则,并且您不需要在Apache中设置404处理(甚至在您的图像/资源目录中)。

Credit goes to the Zend Framework authors that recommend this bit in their user guide. 感谢Zend Framework的作者在他们的用户指南中推荐这一点。 This version is slightly modified for CodeIgniter. 对于CodeIgniter,此版本稍作修改。

This is the .htaccess file I use for my CI install: 这是我用于CI安装的.htaccess文件:

If you have it in a subfolder you will need to edit RewriteBase. 如果您在子文件夹中有它,则需要编辑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> 

Here is complete solution which i am using and found working nicely: 这是我正在使用的完整解决方案,并且发现效果很好:

Place this code snippet below in .htaccess file at your project root like http://localhost/myProject 将此代码段放在项目根目录下的.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]

And at your CI config file located at system\\application\\config\\config.php 并在位于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]

I use this and it work perfectly! 我使用它,它完美地工作!

Insert the following into your .htaccess 将以下内容插入.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