如何解决没有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;
}
}
,
只需更改一行即可轻松修复。不需要额外的块。您只需要在 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/your-site.com 中更改这一行。
将位置/指令尝试文件更改为:
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
希望这对你也有用:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。