【问题标题】:How to configure the URLmanager rules correctly?如何正确配置 URLmanager 规则?
【发布时间】:2014-05-13 09:30:44
【问题描述】:

我在 Yii 中无法配置路由项目是 3 天,所有尝试帮助... 我有:

/var/www/ - 项目

/var/www/yii - yii 框架

./htaccess

RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is http://localhost/begin/, use /begin/

RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]

index.php 找到。

./var/www/protected/config/main.php

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName' => false,
            'rules'=>array(
              '/'=>'/view',
              '//'=>'/',
              '/'=>'/',
            ),
    ),

当我输入例如 107.170.91.28/site/contact 时,会显示错误 404。 服务器地址 107.170.91.28 在本地服务器上。一切正常。 如果我输入此 URL 107.170.91.28/index.php/site/contact 或 107.170.91.28/index.php?r=site/contact - 它可以工作。 寻求帮助,因为我尝试了所有设置的方法。

【问题讨论】:

  • 将 .htaccess 放在 index.php 所在的位置
  • 你为什么改变你的url规则?
  • /var/www/.htaccess 与 /var/www/index.php 在同一文件夹中
  • 我试过了,但我没有正确配置。在本地服务器上有这样的规则,一切正常。

标签: php .htaccess yii yii-url-manager


【解决方案1】:

如果这种情况只发生在服务器上,要么是因为您已将 Apache 配置为忽略服务器上的 .htaccess 指令,要么是因为您没有加载/安装 Rewrite 模块

您可以通过更改 /etc/apache2/apache2.conf 来配置 apache conf 以打开(位置可能因操作系统而异,您当然也可以只为应用程序目录打开它

<Directory>
...
        AllowOverride None
....
</Directory>

<Directory>
...
        AllowOverride All
....
</Directory>

为了启用重写模块,您可以执行(可能因操作系统而异)

a2enmod rewrite

之后重启apache2

/etc/init.d/apache2 restart

service apache2 restart

【讨论】:

  • 非常感谢您的帮助!唯一出现的其他错误(
  • np;如果答案对您有帮助,您可以选择它,那么其他有类似问题的人将知道如何解决它:)
  • 顺便说一句,我不确定您的规则数组是否正常工作。我通常使用这组规则,也许这会解决您的其他问题 "'/'= >'/view', '//'=>'/', ' /'=>'/',"
  • 感谢您的评论,但没有任何改变。我改变了规则。
  • 有趣的是,这是由于不正确的“规则”或链接 Yii::app()-request-> > baseUrl?
猜你喜欢
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 2021-05-04
相关资源
最近更新 更多