yii2高级模板使用一个域名管理前后台(亲测可用)

1.修改 advanced/backend/config/main.PHP 文件如下:

[php]  view plain  copy
  1. return [  
  2.     'homeUrl' => '/admin',  
  3.     'components' => [  
  4.         'request' => [  
  5.             'baseUrl' => '/admin',  
  6.         ],  
  7.         'urlManager' => [  
  8.             'enablePrettyUrl' => true,  
  9.             'showScriptName' => false,  
  10.         ],  
  11.     ],  
  12. ];  

2.同样修改 advanced/frontend/config/main.php 文件:

[php]  view plain  copy
  1. return [  
  2.     'homeUrl' => '/',  
  3.     'components' => [  
  4.         'request' => [  
  5.             'baseUrl' => '',  
  6.         ],  
  7.         'urlManager' => [  
  8.             'enablePrettyUrl' => true,  
  9.             'showScriptName' => false,  
  10.         ],  
  11.     ],  
  12. ];  

3.配置域名解析:

[php]  view plain  copy
  1. <VirtualHost *:80>  
  2.     ServerName advanced.loc  
  3.     ServerAlias www.advanced.loc  
  4.   
  5.     DocumentRoot "/path/to/advanced"  
  6.     <Directory "/path/to/advanced">  
  7.         AllowOverride All  
  8.     </Directory>  
  9. </VirtualHost>  

4.新建一个.htaccess文件,写入一下内容。放在项目根目录advacnced下:

[php]  view plain  copy
  1. # prevent directory listings  
  2. Options -Indexes  
  3. # follow symbolic links  
  4. Options FollowSymlinks  
  5. RewriteEngine on  
  6.   
  7. RewriteCond %{REQUEST_URI} ^/admin/$  
  8. RewriteRule ^(admin)/$ /$1 [R=301,L]  
  9. RewriteCond %{REQUEST_URI} ^/admin  
  10. RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]  
  11.   
  12. RewriteCond %{REQUEST_URI} ^.*$  
  13. RewriteRule ^(.*)$ /frontend/web/$1  

5.再次新建一个.htaccess文件,写入以下内容,在frontend/web和backend/web下分别放一个。
[php]  view plain  copy
  1. use mod_rewrite for pretty URL support  
  2. RewriteEngine on  
  3. if a directory or a file exists, use the request directly  
  4. RewriteCond %{REQUEST_FILENAME} !-f  
  5. RewriteCond %{REQUEST_FILENAME} !-d  
  6. # otherwise forward the request to index.php  
  7. RewriteRule . index.php  

如果服务器是nginx,则更改nginx.cong文件,写入以下内容,具体路径根据自身实际情况进行修改:

[plain]  view plain  copy
  1. server {  
  2.     charset      utf-8;  
  3.     client_max_body_size  200M;  
  4.   
  5.     listen       80; ## listen for ipv4  
  6.     #listen       [::]:80 default_server ipv6only=on; ## listen for ipv6  
  7.   
  8.     server_name  advanced.loc;  
  9.     root         /path/to/advanced;  
  10.   
  11.     access_log   /path/to/logs/advanced.access.log main buffer=50k;  
  12.     error_log    /path/to/logs/advanced.error.log warn;  
  13.   
  14.     location / {  
  15.         root  /path/to/advanced/frontend/web;  
  16.   
  17.         try_files  $uri /frontend/web/index.php?$args;  
  18.   
  19.         # avoiding processing of calls to non-existing static files by Yii  
  20.         location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {  
  21.             access_log  off;  
  22.             expires  360d;  
  23.   
  24.             try_files  $uri =404;  
  25.         }  
  26.     }  
  27.   
  28.     location /admin {  
  29.         alias  /path/to/advanced/backend/web;  
  30.   
  31.         rewrite  ^(/admin)/$ $1 permanent;  
  32.         try_files  $uri /backend/web/index.php?$args;  
  33.     }  
  34.   
  35.     # avoiding processing of calls to non-existing static files by Yii  
  36.     location ~ ^/admin/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {  
  37.         access_log  off;  
  38.         expires  360d;  
  39.   
  40.         rewrite  ^/admin/(.+)$ /backend/web/$1 break;  
  41.         rewrite  ^/admin/(.+)/(.+)$ /backend/web/$1/$2 break;  
  42.         try_files  $uri =404;  
  43.     }  
  44.   
  45.     location ~ \.php$ {  
  46.         include  fastcgi_params;  
  47.         # check your /etc/php5/fpm/pool.d/www.conf to see if PHP-FPM is listening on a socket or port  
  48.         fastcgi_pass  unix:/var/run/php5-fpm.sock; ## listen for socket  
  49.         #fastcgi_pass  127.0.0.1:9000; ## listen for port  
  50.         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;  
  51.         try_files  $uri =404;  
  52.     }  
  53.     #error_page  404 /404.html;  
  54.   
  55.     location = /requirements.php {  
  56.         deny all;  
  57.     }  
  58.   
  59.     location ~ \.(ht|svn|git) {  
  60.         deny all;  
  61.     }  
  62. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值