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 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题