如何在yii2中更改基本URL并启用prettyUrl

时间:2016-05-26 03:58:15

标签: yii2

如何将基本网址 public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 更改为此类http://mydomin/ProjectName/frontend/web/index.php?r=product/index?我阅读了很多关于这个主题的文章,但没有一个证明有用。我也尝试了以下内容:

(1)在frontend / config / main.php的配置文件中启用prettyUrl,我在文章中读到了一些规则..

http://mydomin/product/index

(2)我在/frontend/web/.htaccess的web目录下创建了一个.htaccess文件,并添加了以下代码:

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

现在,当我尝试访问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 时 我收到错误消息:在此服务器上找不到请求的URL http://mydomin/ProjectName/frontend/web/product/index。如果我注释掉/ProjectName/frontend/web/product,则在urlManager设置中,它会以此格式运行'showScriptName' => false 它除了http://mydomin/ProjectName/frontend/web/index.php/product/index,但仍然有?r=文件。 我怎么能隐藏这个文件,甚至隐藏所有应用程序文件夹,如index.php,所以最后而不是projectName, frontend,and web folderhttp://mydomin/ProjectName/frontend/web/index.php/product/index我会得到http://mydomin/ProjectName/frontend/web/index.php?r=product/index

2 个答案:

答案 0 :(得分:1)

1-将此代码放在yii2advance文件夹中的.htaccess flie中(项目的主文件夹)

var list= dbContext.Events
           .Where(e=>EntityFunctions.AddSeconds(e.EndDate, e.EndTo.Value.TotalSeconds) > DateTime.Now)
           .ToList();

以上代码转换

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^admin(/.+)?$ backend/web/$1 [L,PT]
RewriteRule ^(.+)?$ frontend/web/$1

'localhost/yii2advance/frontend/web/index.php'

并转换

'localhost/yii2advance/'

'localhost/yii2advance/backend/web/index.php'

2-将此代码添加到frontend / .htaccess和backend / .htaccess文件中:

上的RewriteEngine
'localhost/yii2advance/admin'

3-在backend / config / main.php中输入以下代码:

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

4-在frontend / config / main.php中输入以下代码:

'homeUrl' => '/yii2advance/admin',

'components' => [
'request' => [
            'baseUrl' => '/yii2advance/admin', // localhost/yii2advance/admin
        ],
    '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>',
            ],
],

答案 1 :(得分:0)

如果您使用advanced-yii2模板,则必须为yor apache创建虚拟主机,如下所示:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName yourlocalhost.ru

    DocumentRoot /var/www/yourlocalproject/frontend/web
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/yourlocalproject/frontend/web>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

yourlocalhost.ru - 您的本地主机名 yourlocalproject - yii项目目录

不要忘记将yourlocalhost.ru添加到hosts文件并重新启动apache