【问题标题】:when removing index.php from url ( codeigniter ) .htaccess Prevent the files from the display从 url (codeigniter) 中删除 index.php 时 .htaccess 防止显示文件
【发布时间】:2012-04-22 19:27:17
【问题描述】:

我正在使用 codeigniter,我尝试通过创建 .htaccess 文件从 url 中删除 index.php 它的内容:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

这种方式适用于 xampp 服务器。 但是当我上传网站时,不幸的是出现了一个问题:

我有一个名为 files 的文件夹,它包含子文件夹:images、css、js、swf、upload。 并且这些文件夹中的每个文件都无法查看,并且浏览器说找不到文件。

请提供任何帮助。

【问题讨论】:

  • 错误日志告诉你什么?

标签: .htaccess codeigniter


【解决方案1】:

只要确保你也排除那些:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css|swf|upload)
RewriteRule ^(.*)$ /index.php/$1 [L]

【讨论】:

    【解决方案2】:

    这是我的 .htaccess 文件。我的理解是,对于任何已尝试访问但不存在的文件或目录,它都会转发到 Codeigniter。

    任何确实存在的东西都可以正常工作,不需要单独排除。这将节省您每次添加新目录或文件夹时的时间和麻烦,因为它不需要您编辑此文件。

    RewriteEngine on
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    

    对我们有用,但我的理解可能不正确。希望对你有帮助

    【讨论】:

    • 谢谢。这工作得很好。我有另一个网站和下一个 index.php 我有一个名为 files 的文件夹,这个文件夹中有很多内容我应该把它们的所有名称和子目录的名称都放在其中吗?
    • 不知道为什么这没有得到更多的支持,这是迄今为止最好的解决方案。
    【解决方案3】:

    试试这个我认为可以解决你的问题..

    RewriteEngine on
    RewriteCond $1 !^(index\.php|robots\.txt|files\/images|files\/css|files\/js|files\/swf|files\/upload)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    您必须告诉您的服务器不要将这些目录处理到 index.php 并且为此工作的行是:

    RewriteCond $1 !^(index\.php|robots\.txt|files\/images|files\/css|files\/js|files\/swf|files\/upload)
    

    【讨论】:

    • 感谢所有我使用最后一个和工作文件。但我应该把所有的子目录?因为我有另一个站点,我有一个包含很多子目录的文件夹,我试图将存在的主目录的名称放在 index.php 附近,但它不起作用。
    • 是的,您应该包含所有您不希望 php 处理的子文件夹/文件夹..
    【解决方案4】:

    这是我的 .htaccess 文件 - 应该适合你:

    Options -Indexes
    Options +FollowSymLinks
    
    # Set the default file for indexes
    DirectoryIndex index.php
    
    <IfModule mod_rewrite.c>
    
        # activate URL rewriting
        RewriteEngine on
    
        # do not rewrite links to the documentation, assets and public files
        RewriteCond $1 !^(files|css|js|swfimages|assets|uploads|captcha)
    
        # do not rewrite for php files in the document root, robots.txt or the maintenance page
        RewriteCond $1 !^([^\..]+\.php|robots\.txt|crossdomain\.xml|maintenance\.html)
    
        # but rewrite everything else
        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.
        ErrorDocument 404 index.php
    </IfModule>  
    

    【讨论】:

      【解决方案5】:

      谢谢。这很好用:

      RewriteEngine on
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /index.php/$1 [L]
      

      我有另一个网站和下一个 index.php 我有一个名为 files 的文件夹,这个包含很多文件夹的内容我应该将它们的所有名称和子目录的名称放在其中吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 2011-05-20
        • 2017-03-15
        相关资源
        最近更新 更多