【问题标题】:.htaccess mod_rewrite dynamic match with Yii generated urls.htaccess mod_rewrite 动态匹配 Yii 生成的 url
【发布时间】:2013-10-02 16:35:24
【问题描述】:

我正在尝试使用 Apache 的 mod_rewrite 模块将来自 Yii 应用程序(始终包含 index.php 作为路由器/引导程序)动态生成的 url 通过 .htaccess 转换为静态 url,可通过 Web 浏览器进行浏览。

示例:localhost/projectname/index.php/commentfeed.xml是动态生成的页面,但我希望它能够通过localhost/projectname/commentfeed.xml访问

我正在运行的 Apache 版本是:Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.0 通过 XAMPP 用于我的开发平台。 mod_rewrite 模块存在并加载在 Apache 的 httpd.conf 文件中。

我在localhost/projectname/中的.htaccess如下:

#Turning on the rewrite engine is necessary for the following rules and features.
#FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.so>
    Options +FollowSymlinks
    RewriteEngine On

#Unless an explicit file or directory exists,
#redirect all request to Yii entry script.

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
</IfModule>  

现在,当我尝试通过没有 index.php (localhost/projectname/commentfeed.xml) 的浏览器访问 commentfeed.xml 时,我收到 404 错误 - 找不到对象!。

我查看了以下指南和 SO 问题:URL Rewriting Guidemod_rewrite documentationSO: Tips for debugging .htaccess rewrite rules,但它们都没有明确说明我在寻找什么。此外,我的 apache 错误日志没有报告任何关于 .htaccess 文件的信息。对于正确使用的任何帮助或指导将不胜感激。

【问题讨论】:

    标签: apache .htaccess mod-rewrite yii


    【解决方案1】:

    我的问题的具体答案在于使用&lt;IfModule&gt; ... &lt;/IfModule&gt;。通过我设置的 XAMPP v3.2.1 和安装了它的 apache 服务器,我在 .htaccess 中需要的唯一代码行很简单:

    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php
    

    【讨论】:

      【解决方案2】:

      你需要告诉 Yii UrlManager 脚本名称不是必需的。在您的 config/main.php 文件中,您应该有:

      'components'=>array(
          ...
          'urlManager'=>array(
              'urlFormat'=>'path',
              'showScriptName'=>false,
              'rules'=>array(
                  ...
              ),
          ),
      ),
      

      确保您还定义了一个路由来指定生成 commentfeed.xml 文件的控制器/操作。

      更多信息:http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x

      【讨论】:

      • 那行是我的 /protected/config/main.php ,它仍然给我一个 404 错误,找不到对象。我偷偷怀疑这与 xampp 或我的本地 apache 服务器有关。谢谢你的建议!
      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-04-28
      • 2015-08-19
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多