没有php扩展名和漂亮的url nginx配置替代

时间:2020-10-07 06:43:17

标签: .htaccess nginx nginx-config

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

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

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

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]

2 个答案:

答案 0 :(得分:2)

尝试一下

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;
    }

}

答案 1 :(得分:0)

只需更改一行即可轻松修复。不需要额外的块。您只需要在 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/your-site.com 中更改这一行。

将位置/指令尝试文件更改为:

location / {
       try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}

希望这对你也有用:)

相关问题