【问题标题】:pretty url not working漂亮的网址不起作用
【发布时间】:2015-10-17 12:41:46
【问题描述】:

下面是我的main.php

 'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [  '<controller:\w+>/<id:\d+>' => '<controller>/view',
                       '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                       '<controller:\w+>/<action:\w+>' => '<controller>/<action>',               
                    ],
        ],

我启用了漂亮的 url(我认为),我收到了很多 404,比如

192.168.1.3/~user/urshow/frontend/web/movies/movies_all 如果是这样192.168.1.3/~user/urshow/frontend/web/index.php?r=movies/movies_all 就可以了 并且没有以前完美的链接正在工作。

【问题讨论】:

    标签: .htaccess yii2 friendly-url yii2-advanced-app


    【解决方案1】:

    在路由器配置中添加以下语句以使 html5 为真。

    $locationProvider.html5Mode({
            enabled: true,
            requireBase: false
        });
    

    然后在 web.config 上添加设置

      <system.webServer>
        <rewrite>
          <rules>
            <clear />
            <rule name="AngularJS" enabled="true" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
              </conditions>
              <action type="Rewrite" url="/index.html" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    

    【讨论】:

      【解决方案2】:

      我的 .htacess 文件位于我项目的 web 文件夹中

      RewriteEngine on
      RewriteBase /~user/urshow/frontend/web/
      # 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
      

      我在配置中的 main.php 文件

       'urlManager' => [
              'class' => 'yii\web\UrlManager',
              'enablePrettyUrl' => true,
              'showScriptName' => false,
               'rules' => array(
                  '<controller:\w+>/<id:\d+>' => '<controller>/view',
                  '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                  '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
          ),
          ],
      

      这对我有用。

      【讨论】:

        【解决方案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
        

        现在在您的 web.php 中的组件部分添加以下内容:

        'urlManager' => [
                'class' => 'yii\web\UrlManager',
                // Disable index.php
                'showScriptName' => false,
                // Disable r= routes
                'enablePrettyUrl' => true,
                'rules' => array(
                        '<controller:\w+>/<id:\d+>' => '<controller>/view',
                        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                ),
                ],
        

        这肯定会奏效。

        【讨论】:

        • 我同意,这应该可以,尽管 .htaccess 文件不应放在项目文件夹中,而应放在应用程序的 web/ 文件夹中,您的 index.php 文件所在的位置
        • 它没有用。我收到这样的错误-无法解析请求〜user/urshow/frontend/web/movies/movies_all
        • 你把 .htaccess 文件放在哪里了?
        • 我放在了 urshow 文件夹(我的应用程序文件夹)。我收到这样的错误无法解决请求“~user/urshow/frontend/web/movies/movies_all”。如果将 index.php 放在 /web 之后,它将起作用。它只是'index.php'不起作用
        • 将此替换为 .htaccess 文件 RewriteRule 的最后一行。 ^(.+)$ index.php?$1 [PT,L,QSA] 希望这个工作顺利
        【解决方案4】:

        我认为这会奏效。 . .

        192.168.1.3/~user/urshow/frontend/web/index.php/movies/movies_all
        

        将此代码放入common/config/main.php

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [  '<controller:\w+>/<id:\d+>' => '<controller>/view',
                           '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                           '<controller:\w+>/<action:\w+>' => '<controller>/<action>',               
                        ],
            ],
        

        【讨论】:

        【解决方案5】:

        试试这个... 在您的根文件夹中创建 .htaccess 文件 并添加以下代码

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php)
        RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]
        

        可以帮助你..

        【讨论】:

          猜你喜欢
          • 2017-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-11
          • 2015-11-24
          • 2015-06-01
          • 1970-01-01
          相关资源
          最近更新 更多