【问题标题】:Codeigniter htaccess not workingCodeigniter htaccess 不工作
【发布时间】:2012-04-29 20:12:39
【问题描述】:

我在我的网站上使用这个 htaccess 文件来删除 index.php。

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

但是这个文件不起作用。在 Apache 模块中启用了重写。使用的 codeigniter 版本是 2.0.1。

【问题讨论】:

  • 尝试删除第一个RewriteCond并将RewriteRule模式更改为^(?!index\.php.*)$
  • 你要我这样做吗? RewriteRule 上的 RewriteEngine ^(.*)$ ^(?!index\.php.*)$
  • 不,RewriteRule ^(?!index\.php.*)$ index.php/$1 [L,QSA]。并删除RewriteCond $1 !^(index\.php)

标签: php .htaccess codeigniter


【解决方案1】:

我已经在没有 codeigniter 的情况下对此进行了测试,它可以工作。

我在 htdocs 下创建了一个名为“demo”的文件夹。我将“.htaccess”与您的内容+附加行(RewriteBase)一起放在那里:

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

然后是下面的“index.php”:

<?php
echo $_SERVER['PATH_INFO'];

测试: http://localhost/demo/hello

结果: /你好


检查事项:

  • 如果您将“.htaccess”放在文件夹中,请不要忘记添加 重写基础。
  • 确保为您的目录启用了 FollowSymLinks。请参阅this link 了解更多信息。

【讨论】:

  • 是的,我面临的问题是 RewriteBase。非常感谢您的解决方案。
  • 嗨 Muhammad,我已经尝试过了,但是在我的域上这也不起作用,我在 hostgator 共享主机上,如果我使用它,则会出现 500 错误,我的其他域没有提供任何错误,你能建议是什么原因吗?
【解决方案2】:

阅读this

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

【讨论】:

  • 试试这个 {RewriteEngine on RewriteRule ^(.*)$ index.php/$1 [L] }
【解决方案3】:

Codeigniter 3

的解决方案可能很晚

把它放在你的根目录

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

转到 apache/apache_version/conf/httpd.conf(我正在使用 Laragon)

放在httpd.conf的最后

Listen 9595
<VirtualHost *:9595>
DocumentRoot "C:\laragon\www\capstone_project"
<Directory "C:\laragon\www\capstone_project">
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>

重启你的 apache 看看它是否工作。

【讨论】:

    【解决方案4】:

    将其复制到您的 .htaccess 中

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /CI_FOLDER/
    #CI_FOLDER is the Location of your CI files.
    
    #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>  
    

    【讨论】:

      【解决方案5】:

      将此代码放入CI_Folder的.htaccess文件中

       <IfModule mod_rewrite.c> 
          Options +FollowSymLinks 
          RewriteEngine on 
          RewriteBase /foldername/ 
          RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d 
          RewriteRule ^(.*)$ /foldername/index.php?/$1 [QSA,L] 
          </IfModule>
          # BEGIN Expire headers <ifModule mod_expires.c>
              ExpiresActive On
              ExpiresDefault "access plus 5 seconds"
              ExpiresByType image/x-icon "access plus 2592000 seconds"
              ExpiresByType image/jpeg "access plus 2592000 seconds"
              ExpiresByType image/png "access plus 2592000 seconds"
              ExpiresByType image/gif "access plus 2592000 seconds"
              ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
              ExpiresByType text/css "access plus 604800 seconds"
              ExpiresByType text/javascript "access plus 216000 seconds"
              ExpiresByType application/javascript "access plus 216000 seconds"
              ExpiresByType application/x-javascript "access plus 216000 seconds"
              ExpiresByType text/html "access plus 600 seconds"
              ExpiresByType application/xhtml+xml "access plus 600 seconds" </ifModule>
          # END Expire headers
      
          # BEGIN Cache-Control Headers <ifModule mod_headers.c>
              <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
                  Header set Cache-Control "public"
              </filesMatch>
              <filesMatch "\.(css)$">
                  Header set Cache-Control "public"
              </filesMatch>
              <filesMatch "\.(js)$">
                  Header set Cache-Control "private"
              </filesMatch>
              <filesMatch "\.(x?html?|php)$">
                  Header set Cache-Control "private, must-revalidate"
              </filesMatch> </ifModule>
          # END Cache-Control Headers
          # BEGIN GZIP COMPRESSION <IfModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </IfModule>
          # END GZIP COMPRESSION
      

      【讨论】:

      • 虽然这段代码可能会回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用处。
      【解决方案6】:

      检查您的 Apache 站点配置文件。在 Ubuntu 14.04 上,该文件为 /etc/apache2/sites-available/site.conf

      这样换行

      AllowOverride None
      

      到这里

      AllowOverride All
      

      重启 Apache 以加载更新后的 site.conf

      sudo service apache2 restart
      

      这为我解决了。

      【讨论】:

        猜你喜欢
        • 2016-03-01
        • 2011-05-29
        • 2015-12-16
        • 1970-01-01
        • 2018-03-19
        • 1970-01-01
        • 2015-08-11
        • 2020-03-02
        • 1970-01-01
        相关资源
        最近更新 更多