.htaccess - 没有 php 扩展和漂亮的 url nginx 配置替代
问题描述
我刚刚将我的代码从 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]
解决方案
试试这个
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;
}
}
推荐阅读
- node.js - 如何从nodejs中的mongoDB时间戳字符串中仅提取日月和小时
- javascript - Chrome DevTool 移动模拟器中未触发 touchmove
- vb.net - 从字符串转换日期时间时转换失败
- angular - 如何在 Angular 项目中删除 Angular Universal
- react-native - 试图注册两个同名的视图 RNSVGRect
- vb.net - 我如何将一个文本框的数量与另一个文本框 VB.NET 相加
- javascript - Oncuechange事件触发html的P标签内的淡入淡出动画
- azure - 从 Azure Pipeline 和 powershell 脚本以管理员身份运行 .exe 文件
- conv-neural-network - 对列表运行 conv2d
- python - 如何在 HTML 中嵌入 python 文件或代码?