NGINX configuration YII

本文介绍如何配置Nginx服务器以实现Yii框架项目的SEO友好URL。通过特定的Nginx配置规则,可以隐藏index.php并实现路径风格的URL。此外,还提供了Yii配置文件示例,展示了如何设置urlManager组件以启用路径格式的URL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自于 https://www.nginx.com/resources/wiki/start/topics/recipes/yii/

Recipe

This configuring gives SEF URLs for Yii

NGINX configuration

server {
        server_name domain.tld;

        root /usr/share/nginx/html;
        index index.html index.php;

        #Yii Specific location configurations.

        #SEF URLs for sampleapp. 关键是这个
        location /sampleapp {
         rewrite ^/sampleapp/(.*)$ /sampleapp/index.php?r=$1;
        }

        location ~ /(protected|framework|nbproject) {
            deny all;
            access_log off;
            log_not_found off;
        }

        location ~ /themes/\w+/views {
            deny all;
            access_log off;
            log_not_found off;
        }

        location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
                try_files $uri =404;
        }

        #End Yii Specific specific location configurations.

        location ~ \.php$ {
                root            /usr/share/nginx/html;
                fastcgi_pass    127.0.0.1:9000;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include         fastcgi_params;
        }


}

Yii confugration

After this, make sure that your Yii configuration file (main.php) for your app( sampleapp) contains the following information to enable SEF URLs in the app:

'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
        ),
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值