执行尾部斜杠,并使用放在子文件夹中的htaccess删除index.php

时间:2012-08-10 17:17:17

标签: .htaccess codeigniter mod-rewrite

我有一个CodeIgniter项目放在域的子文件夹中。 对于任何涉及CodeIgniter目录的url,我想让我的.htaccess(放在CodeIgniter子文件夹中)执行以下操作:

  1. 从任何网址中删除“index.php”。
  2. 始终向任何网址添加尾部斜杠。
  3. 目前我的.htaccess看起来像这样:

    
        RewriteEngine on
        RewriteBase /ci_folder/        #This is the CI Subfolder
    
        # Get rid of index.php
        RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    
        # Add trailing slash
        RewriteCond %{REQUEST_URI} !(.*)/$
        RewriteRule ^(.*[^/])$ $1/ [L,R=301]
    

    问题是,它只能部分工作; index.php被删除很好,但是当添加尾部斜杠时,不是重定向到“固定”URL,而是重定向到本地路径fx。

    domain.com/ci_folder/method

    被重定向到:

    domain.com/home/sites/domain.com/public_html/ci_folder/index.php/method /

    应该是:domain.com/ci_folder/method/! (也不包括任何index.php)

    ---编辑---

    这对我有用:

    
        RewriteEngine on
        RewriteBase /ci_folder/
    
        RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
        RewriteCond %{REQUEST_FILENAME} -f [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [L]
    
        RewriteCond %{REQUEST_URI} !(.*)/$
        # dont rewrite if there was posted here!
        RewriteCond %{REQUEST_METHOD} !POST 
        RewriteRule ^(.*[^/])$ $1/ [L,R=301]
    
        RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    
    

1 个答案:

答案 0 :(得分:3)

RewriteEngine on
RewriteBase /ci_folder/

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]

RewriteCond %{REQUEST_URI} !(.*)/$
# dont rewrite if there was posted here!
RewriteCond %{REQUEST_METHOD} !POST 
RewriteRule ^(.*[^/])$ $1/ [L,R=301]

RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]