doubinduo3364 2014-05-07 19:48
浏览 68
已采纳

Godaddy上的CodeIgniter安装具有最佳安全性

On godaddy hosting public_html is given as a web root. I'm trying to install CodeIgniter on it so I'd like the whole framework to be outside of webroot (for security reasons). For this specific purpose, in the public_html directory I've created .htaccess with the following code:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub_webroot/
RewriteRule ^(.*)$ ./sub_webroot/index.php?$1 [L]

Directory/file structure looks like this:

public_html
    .htaccess
    CodeIgniter (whole framework files except index.php)
    sub_webroot
        index.php (CI index.php)
        assets
             sample.png

The framework is loaded successfully and index.php is removed as well. The problem which I am facing is that I can't open sample.png via example.com/assets/sample.png and it is obvious it is happening because of the line RewriteRule ^(.*)$ ./sub_webroot/index.php?$1 [L]. I can't made up my mind how it would be possible to access the assets directory and keep the framework working successfully as it is working now. Any ideas how to change .htaccess that meets my needs ?

  • 写回答

1条回答 默认 最新

  • duanpei8518 2015-07-07 08:11
    关注

    This is how we solved this problem: adding a condition to ignore the rewrite if requesting from the assets folder. You can add/remove options to ignore as required - really you only need the assets option in your case.

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document
    RewriteCond $1 !^(index\.php|assets|css|png|jpg|gif|robots\.txt|favicon\.ico)
    

    Place this before your RewriteRule.

    There's a pretty comprehensive Codeigniter .htaccess for troublesome hosts at: http://www.chrishjorth.com/blog/one-com-codeigniter-htaccess-rewrite-rules/, that's nicely commented:

    # @author: Chris Hjorth, www.chrishjorth.com
    # Make index.php the directory index page
    DirectoryIndex index.php
    #Protect the .htaccess files
    <Files .htaccess>
        order allow,deny
        deny from all
    </Files>
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /subfolder/
        # START CodeIgniter ------------------------------------------------------------------------------------------------------------
        # based on http://www.danielwmoore.com/extras/index.php?topic=7691.0 and http://ellislab.com/forums/viewthread/132758/
        # Redirect default controller to "/".
        # This is to prevent duplicated content. (/welcome/index =&gt; /)
        RewriteRule ^(welcome(/index)?)/?$ /subfolder/ [L,R=301]
        # Remove /index/ segment on the URL, again to prevent duplicate content.
        RewriteRule ^(.*)/index/? $1 [L,R=301]
        # Remove trailing slashes, also to remove duplicate content
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Remove multiple slashes in between, just to remove the possibility of fabricating crazy links.
        RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
        RewriteRule . %1/%2 [R=301,L]
        # Ignore certain files and folders in this rewrite
        RewriteCond $1 !^(index\.php|assets|frameworks|uploads|robots\.txt|favicon\.ico)
        # [NC] = no case - case insensitive
        # [L] = Last rule, last rewrite for this set of conditions
        # [QSA] = Query String Append, should be used to prevent all redirects from going to your default controller, which happens on 
        # some server configurations.
        RewriteRule ^(.*)$ /subfolder/index.php?$1 [NC,L,QSA]
        # END CodeIgniter --------------------------------------------------------------------------------------------------------------
    </IfModule>
    # If Mod_rewrite is NOT installed go to index.php
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /index.php
    </IfModule>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献