【问题标题】:.htaccess relative mod_rewrite path - simplify deployment of an application.htaccess 相对 mod_rewrite 路径 - 简化应用程序的部署
【发布时间】:2012-04-25 23:04:24
【问题描述】:

每次部署 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 not found 错误。 尝试了很多不同的组合,并希望看到您的建议。谢谢!

【问题讨论】:

  • 不是这样的答案,但我更喜欢在我的本地开发实例上设置虚拟主机并在主机文件中创建条目,以便可以使用名称访问它们。这意味着您可以保持目录结构与它在生产中的方式相同,这都不是问题。
  • 另外,您可以删除 RewriteBase 并使用相对路径执行所有操作(不是吗?我想不出它不起作用的原因,但其他人可能有)
  • @DaveRandom 你完全正确,请发表你的评论作为答案,以便我接受。删除 RewriteBase 然后将所有内容指向 index.php 使其完美运行。非常感谢!
  • 我也设置了一个本地虚拟主机进行测试。如果我开发一个网站,例如example.com 我在我的 dev-pc 的主机文件 127.0.0.1 example 中创建一个条目,并为 example 域创建一个虚拟主机。

标签: php .htaccess codeigniter relative-path absolute-path


【解决方案1】:

我个人不喜欢您在 .htaccess 中拒绝系统和应用程序文件夹。

根据此处的“CI 最佳实践”指南 (http://codeigniter.com/forums/viewthread/125687/) - 您应该将应用程序和系统文件夹移出 public_html,只留下 index.php。然后不管人们尝试什么花哨的东西 - 他们无法从 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/project1 , localhost/project2

只需将 htaccess 文件的副本放在每个项目的根目录中即可。我的根本地主机中没有这个文件

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多