将htaccess与Codeigniter和特定规则结合使用

时间:2019-04-01 11:03:45

标签: php .htaccess codeigniter

我无法向存在Codeigniter重写规则的.htaccess文件中添加特定的重写规则。

              Options -Indexes
      Options +FollowSymLinks



      <IfModule mod_rewrite.c>
         RewriteEngine On
         RewriteCond %{HTTPS} off
         RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}

         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system|sayfalar|main\.php)
         RewriteRule ^(.*)$ /index.php?/$1 [L]


        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^main/(.*)$ /main.php?tag=$1 [L,NC]

        DirectoryIndex home.php
      </IfModule>

RewriteRule ^main/(.*)$ /main.php?tag=$1 [L,NC]节根本不起作用

当我键入与main.php?tag = decoration相对应的site.com/main/decoraion时,出现了Codeigniter的404错误页面。虽然我告诉htaccess在重写时不要考虑main.php文件,但Codeigniter的规则可以处理请求

添加此规则的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

您应该交换两个规则:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}

    # => Put your specific rule here !
    RewriteRule ^main/(.*)$ /main.php?tag=$1 [L,NC]

    # The remaining route, acting as a "catch-all"
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # I'm not really sure the purpose of your next condition, but I suspect it's useless. See comment.
    #RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system|sayfalar|main\.php)
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    DirectoryIndex home.php
</IfModule>

提醒您,[L]表示该规则应该是最后一条适用的规则。

因此,当您完成RewriteRule ^(.*)$ /index.php?/$1 [L]时,您告诉Apache匹配任何URL并将其重写为索引,而不要查找其他规则。因此,您的问题。

因此,主要的收获是在末尾重写为索引(充当“包罗万象”的角色),并在前面添加特定的路由。