漂亮的网址不起作用

时间:2015-07-27 10:54:51

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

以下是我的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>',               
                    ],
        ],

我启用了漂亮的网址(我认为),我得到了很多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一样可以正常工作 没有任何链接工作,以前工作完美。

5 个答案:

答案 0 :(得分:2)

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

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

可以帮助你..

答案 1 :(得分:1)

我认为这会奏效。 。

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>',               
                ],
    ],

答案 2 :(得分:1)

创建.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>',
        ),
        ],

这肯定会奏效。

答案 3 :(得分:0)

我的.htacess文件位于我项目的网络文件夹

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文件在config

 '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>',
    ),
    ],

这对我有用。

答案 4 :(得分:-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>