【问题标题】:yii : how to config SEO friendly url at Yii?yii:如何在 Yii 配置 SEO 友好的 url?
【发布时间】:2012-10-08 12:20:00
【问题描述】:

我通过 yiic 创建了一个应用程序。然后我尝试制作一个 SEO 网址。我在 [app root]/protected/config/main.php 取消注释 urlManager。我将以下内容添加到 [app root]/.htaccess 中。

重写引擎开启

如果目录或文件存在,直接使用

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

否则转发到 index.php

重写规则。 index.php

当我浏览 [app]/site/index 时,我收到错误 404 - 找不到对象。但是如果我通过 [app]/index.php?r=site/index 浏览,它会显示我的应用页面。我通过谷歌搜索跟踪了许多网站。但我得到了同样的错误。

请帮我解决这个问题。

【问题讨论】:

    标签: yii seo


    【解决方案1】:
    Try to use this :
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
    

    您可以在 Yii 中使用 urlManager 来自定义 url 规则,而不是使用 .htaccess。在 urlManager 上阅读以了解详细信息。我希望这篇文章也可能有助于给你这个想法。 Controlling url in Yii: controller/action/id to /id

    【讨论】:

    • Ujjwal Prajapati,感谢您的帮助。但我没有运气。同样的404错误。我有100多个控制器。我无法手动管理它们。我该如何解决?
    • 好的。暂时删除 htaccess 以摆脱 404。你能指定你想要的 url 规则吗?我的意思是,如果有控制器而没有控制器,你想要什么。或者您可以做的是拥有 index.php/controller 将带您到控制器页面和 index.php/staticontroller/post 将带您到一个静态控制器来处理没有控制器的帖子。会这样吗?
    • 现在它作为 app/index.php/controller/action 工作。我该怎么做才能喜欢这个应用程序/控制器/动作。
    • 要隐藏 index.php,你必须使用 .htaccess。在您的系统中检查哪些 url 规则有效。你的权利和我的一样。是的,你必须在 urlManager 中设置 showScriptName => false。
    • 'urlManager'=>array('urlFormat'=>'path', 'showScriptName' => false, 'rules'=>array('/'=>'/view', '//'=>'/', '/'=>'/', ), ), 这是我的 urlManager。我将 .htaccess 设置为您的。我怎样才能喜欢这个应用程序/控制器/动作?
    【解决方案2】:

    Check out this article on URL rewriting in Yii

    另外,在您的配置中,将 showScriptName 设置为 false

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

    【讨论】:

      【解决方案3】:

      我自己找到了解决方案。 1. 在 app/protected/config/main.php 取消注释 urlManager 2. 在 app/ 文件夹下创建 .htaccess 文件。 (重要:不在 app/protected/ 文件夹下) 3. 将以下代码复制并粘贴到新的 .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
      

      仅此而已。谢谢大家帮助我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 2012-07-05
        • 2010-11-14
        相关资源
        最近更新 更多