yii2 url重写配置 [英] yii2 url rewrite config

查看:160
本文介绍了yii2 url重写配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是yii2的新手,我已按照说明通过composer安装yii2,但卡在ubuntu服务器上的url重写配置中.

i'm a newbie of yii2, i've follow the instruction to install the yii2 by composer, but stuck in the url rewrite config on ubuntu server.

我的apache2配置文件如下:

my apache2 config file as following:

Alias /math2 /usr/share/math2/frontend/web
Alias /admin /usr/share/math2/backend/web

<Directory "/usr/local/math2/frontend/web">
    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
    RewriteBase /math2
    #RewriteRule . index.php
    RewriteRule ^.*$ index.php [L]
</Directory>

<Directory "/usr/local/math2/backend/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
    RewriteBase /admin
    RewriteRule . index.php
</Directory>

和yii配置文件如下:

and the yii config file as following:

<?php
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false
        ],
    ],
];

我的VirtualHost如下

My VirtualHost as following

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   DocumentRoot /var/www/html

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

我可以通过 http://localhost/math2/index.php/site正常访问该站点/about

但是应该由 http://localhost/math2/site/about 我现在收到404错误.

but it is suppose to be visited by http://localhost/math2/site/about which i get a 404 error now.

推荐答案

我实际上不对虚拟主机使用别名来获取相似的URL.我在项目根目录.htaccess中:

I actually do not use aliases with virtual hosts to get similar urls. I have in project root .htaccess:

Options -Indexes
Options FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/admin/$
RewriteRule ^(admin)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]

RewriteCond %{REQUEST_URI} ^.*$
RewriteRule ^(.*)$ /frontend/web/$1

在前端/网络和后端/网络中,我的.htaccess如下:

In frontend/web and backend/web i have .htaccess like:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

这篇关于yii2 url重写配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆