Codeigniter htaccess 不工作

问题描述 投票:0回答:7

我正在我的网站上使用此 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。

php .htaccess codeigniter
7个回答
7
投票

我已经在没有 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”放入文件夹中,请不要忘记添加 重写Base。
  • 确保您的目录启用了 FollowSymLinks。参见这个 链接了解更多信息。

2
投票

对于 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,看看它是否工作。


1
投票

阅读这个

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

0
投票

将其复制到您的 .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>  

0
投票

将此代码放入 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

0
投票

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

像这样改变线路

AllowOverride None

对此

AllowOverride All

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

sudo service apache2 restart

这为我解决了问题。


0
投票

只需更改默认的 Codedigniter-3.1.11 或等版本,您只需更改文件夹名称,例如:- Codedigniter-3.1.11 到 -> Crud_OP 现在可以使用 .htaccess 文件轻松删除 index.php 文件。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-denter code here
RewriteRule ^(.*)$ index.php/$1 [L]
© www.soinside.com 2019 - 2024. All rights reserved.