apache 重写怎么转 nginx

2015 年 2 月 21 日
 holinhot

RewriteCond %{THE_REQUEST} ^GET\s.+.php [NC]
RewriteRule ^(.+).php$ /$1 [NE,R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

在线转换工具不管用啊

3930 次点击
所在节点    NGINX
17 条回复
holinhot
2015 年 2 月 21 日
大神给看看
zmr90
2015 年 2 月 21 日
http://90r.org/628.html 试下里面的网站,我试过还可以的
extreme
2015 年 2 月 21 日
虽然我第一个使用的HTTP Server是Apache,但我开始自己写rewrite却不是Apache,而是Nginx,因此对mod_rewrite的语法不太了解。
也许你可以尝试说明用途(预期效果),而不是给mod_rewrite的规则,说不定别人更容易帮到你。
holinhot
2015 年 2 月 21 日
holinhot
2015 年 2 月 21 日
@louishothot 有利于seo
holinhot
2015 年 2 月 21 日
@extreme 不修改网站源代码的前提下
holinhot
2015 年 2 月 21 日
holinhot
2015 年 2 月 21 日
liuhaotian
2015 年 2 月 21 日
if ($request_filename ~ "(.*)\.php") {
rewrite ^/(.*)\.php$ http://www.hwbj.net/$1 permanent;
}
if ($request_filename !~ "(.*)\.php") {
rewrite ^/(.*)$ /$1.php last;
}
extreme
2015 年 2 月 21 日
@louishothot
试试这个:
rewrite ^/(.*)\.php$ /$1 permanent;
if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if (!-d $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/.*$ $fastcgi_script_name.php?$query_string last;
}
extreme
2015 年 2 月 21 日
@louishothot 刚刚测试了一下,发现一点问题,这个是完善的版本:
rewrite "^/(.*)\.php(/){0,}$" /$1 permanent;
if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if (!-d $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/.*$ $fastcgi_script_name.php last;
}
ryd994
2015 年 2 月 21 日
@liuhaotian
@extreme
诸位,if is evil,各种nginx文档里都已经强调过无数次了
http://wiki.nginx.org/IfIsEvil


location ~ (.+)\.php$ {
return 301 $1;
}

location / {
try_files ${uri}.php =404;
}
extreme
2015 年 2 月 21 日
@ryd994 Amazing!
liuhaotian
2015 年 2 月 21 日
@ryd994 一直不是很懂try_files怎么用,看来要去好好学学了。谢谢@
ryd994
2015 年 2 月 21 日
@liuhaotian try_files就是搜索后面的每个文件(除了最后一个参数),如果存在,则uri变成相应的uri,但仍在当前location继续处理。如果文件都不存在,则直接到最后一个参数所在的location,最后一个参数除了普通uri,也可使用=httpcode和@namedblock
liuhaotian
2015 年 2 月 21 日
@ryd994 这样子 那也就是说try_files可以代替rewrite吗。。
rewrite /(.*)/(.*)$ index.php?c=$1&b=$2;
是不是可以写成
location ~ /(.*)/(.*)${
try_files index.php?c=$1&b=$2 =404;
}
这样子?
ryd994
2015 年 2 月 22 日
@liuhaotian try_files不带get参数

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/171996

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX