.htaccess相对mod_rewrite路径 - 简化应用程序的部署

时间:2012-04-13 14:59:39

标签: php .htaccess codeigniter relative absolute

每次部署CodeIgniter应用程序时,我发现自己都在努力改变.htaccess中的路径。因此;我正在寻找一个适当的,良好的工作解决方案,无论实际项目的位置如何都能正常工作。

障碍:我的本地测试环境不是localhost /,而是localhost / project。这很可能不是现场环境中的情况(更可能是/)。因此,我更喜欢一种无论如何都有效的解决方案。

我现在的.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /project/index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /project/index.php?/$1 [L]

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

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /project/index.php
</IfModule>  

仅使用index.php作为路径将不起作用。它给了我404未找到的错误。 尝试了很多不同的组合,并希望看到你的建议。谢谢!

1 个答案:

答案 0 :(得分:0)

我个人不喜欢你在.htaccess中否认系统和应用程序的凶手。

根据此处的“CI最佳实践”指南(http://codeigniter.com/forums/viewthread/125687/) - 您应该将应用程序和系统文件夹移出public_html,只留下index.php。然后无关紧要人们尝试什么花哨的东西 - 他们无法访问你的应用程序&amp;来自http。

的系统文件夹

然后同时 - 这是我在所有项目中使用的.htaccess文件 - 不需要任何修改。它只是“有效”(对我而言)。

Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c> 
        # activate URL rewriting 
        RewriteEngine on  

        # do not rewrite links to the documentation, assets and public files 
        RewriteCond $1 !^(files|css|js|assets|uploads|captcha)

        # do not rewrite for php files in the document root, robots.txt                  
        RewriteCond $1 !^([^\..]+\.php|robots\.txt) 

        # but rewrite everything else   
        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. 
        ErrorDocument 404 index.php 
  </IfModule>   

哦 - 我将这个用于我在localhost上的多个项目。即localhost / project1,localhost / project2

只需将htaccess文件的副本放在每个项目的根目录中。我不能在root localhost中有这个文件