使用重写规则从网址中删除.php

编程入门 行业动态 更新时间:2024-10-13 04:26:05
本文介绍了使用重写规则从网址中删除.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用nginx重写url,以便可以省略.php扩展名.

I want to rewrite the url in nginx so that the .php extension can be omitted.

这是我所拥有的,但是对我来说不起作用.有人知道如何执行此操作吗?

This is what I have, but this isn't working for me. Anyone any idea how to do this?

谢谢.

server { listen 80; server_name example; return 301 $scheme://www.example$request_uri; } server { listen 80; root /usr/share/nginx/www; index index.php; server_name www.example; error_page 404 www.example/404.php; autoindex off; error_log /usr/share/nginx/www/nginx_error.log warn; location / { rewrite ^(.*)$ /$1.php; } location = / { rewrite ^ /index.php; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; } }

这是我的整个配置文件.

this is my whole config file.

推荐答案

@Makromat我认为您在注释中提到的404问题是您的/位置已解析为/.php而没有文件名,因此解决它,我们可能会添加另一个位置来处理这种特殊情况

@Makromat I think the 404 issue you are mentioning in the comments is that your / location is resolving to /.php without a file name, so to solve it we might add another location just to handle this special case

location / { rewrite ^(.*)$ /$1.php; } location = / { rewrite ^ /index.php; }

尝试一下,告诉我是否可行

Try this and tell me if it works

好吧,我有一个更好的主意,如果原始URL已经包含.php扩展名,第一种情况将失败,因此最好将其定义为后备解决方案,请尝试

Ok scratch that, I have a better idea, the first case will fail if the original URL already contains the .php extension so it's better to define it as a fall back solution, try this

location / { try_files $uri $uri.php $uri/; }

尝试用您当前的php块替换

Try replace this with your current php block

location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; }

更多推荐

使用重写规则从网址中删除.php

本文发布于:2023-11-02 00:28:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   规则   网址   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!