【问题标题】:CodeIgniter Installation on godaddy with best security purposes在 Godaddy 上安装 CodeIgniter,具有最佳安全性
【发布时间】:2023-03-19 02:03:02
【问题描述】:

godaddy 上托管 public_html 以 Web 根目录的形式提供。我正在尝试在其上安装CodeIgniter,因此我希望整个框架位于webroot 之外(出于安全原因)。为此,在public_html 目录中,我使用以下代码创建了.htaccess

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

目录/文件结构如下:

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

框架加载成功,index.php 也被删除。我面临的问题是我无法通过example.com/assets/sample.png 打开sample.png,很明显这是因为RewriteRule ^(.*)$ ./sub_webroot/index.php?$1 [L] 行。我无法下定决心如何访问assets 目录并保持框架像现在一样成功运行。任何想法如何更改 .htaccess 以满足我的需求?

【问题讨论】:

  • 更好的主意,找一个更好的主人,godaddy是最糟糕的之一

标签: php apache .htaccess codeigniter


【解决方案1】:

这就是我们解决这个问题的方法:如果从资产文件夹请求,则添加一个忽略重写的条件。您可以根据需要添加/删除要忽略的选项 - 实际上您只需要 assets 选项。

#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)

把它放在你的 RewriteRule 之前。

有一个非常全面的 Codeigniter .htaccess 用于麻烦的主机:http://www.chrishjorth.com/blog/one-com-codeigniter-htaccess-rewrite-rules/,评论很好:

# @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>

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2023-03-23
    • 2012-06-17
    相关资源
    最近更新 更多