没有 php 扩展名和漂亮的 url nginx 配置替代 [英] No php extension and pretty url nginx config alternative

查看:79
本文介绍了没有 php 扩展名和漂亮的 url nginx 配置替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的代码从 apache 迁移到了 nginx 服务器.

I just migrated my code from apache to nginx server.

我的 apache .htaccess 的替代 nginx 配置是什么.

What would be the alternative nginx config to my apache .htaccess.

我使用的是删除 .php 扩展名和漂亮的 url 重写规则.

What i use are rules for removing .php extension and pretty url rewrite.

RewriteEngine On

#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#for pretty url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]

推荐答案

试试这个

map $uri $pretty_url {
    ~/(.*)$    $1;
}

server {

    ...

    location / {
        index index.php; # replace this to 'index index.php index.html index.htm;' if needed
        try_files $uri $uri/ @extensionless-php;
    }

    location ~ \.php$ {
        # default PHP-FPM handler here
        ...
    }

    location @extensionless-php {
        if ( -f $document_root$uri.php ) {
            rewrite ^ $uri.php last;
        }
        rewrite ^ /index.php?url=$pretty_url last;
    }

}

这篇关于没有 php 扩展名和漂亮的 url nginx 配置替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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