2012-04-18 136 views
2

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

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。

+0

尝试删除第一个'RewriteCond'并将'RewriteRule'模式更改为'^(?! index \ .php。*)$' – DaveRandom 2012-04-18 12:10:54

+0

您是否希望我这样做? RewriteEngine on RewriteRule ^(。*)$ ^(?! index \ .php。*)$ – user1163513 2012-04-18 12:12:38

+0

否,'RewriteRule ^(?! index \ .php。*)$ index.php/$ 1 [L,QSA]' 。并删除'RewriteCond $ 1!^(index \ .php)'。 – DaveRandom 2012-04-18 12:13:50

回答

3

我没有笨进行了测试,它的工作原理。

我在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”,不要忘记添加 RewriteBase。
  • 确保您的目录启用了FollowSymLinks。有关更多信息,请参阅this link
+0

是的,我面临的问题是RewriteBase.Thanks很多解决方案。 – user1163513 2012-04-18 16:54:32

+0

嗨穆罕默德,我已经尝试过这个,但在我的域名莫名其妙地这也无法正常工作,我在hostgator共享主机,如果我使用这个然后得到500错误,我的其他域名不会给出任何错误,请你建议什么会是原因吗? – 2013-03-09 17:27:30

1

阅读this

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

感谢您的回复,但这并不适用于我。 – user1163513 2012-04-18 12:14:47

+0

试试这个{RewriteEngine on RewriteRule ^(。*)$ index.php/$ 1 [L]} – 2012-04-18 12:17:56

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

虽然这段代码可能会回答这个问题,但最好解释它如何解决问题而不介绍其他人以及为什么要使用它。从长远来看,仅有代码的答案是没有用的。 – JAL 2015-11-18 02:48:53

+0

非常感谢你 - 日航 – 2015-11-18 03:16:38

0

检查您的网站的Apache配置文件。在Ubuntu 14.04的文件将是/etc/apache2/sites-available/site.conf

变更线这样

AllowOverride None 

对此

AllowOverride All 

重启Apache以加载更新的网站.conf

sudo service apache2 restart 

这解决了它对我来说。