【问题标题】:nginx php file rewrite urlnginx php文件重写url
【发布时间】:2013-01-12 20:32:24
【问题描述】:

所以使用 apache 我有一个文件夹:

www.domain.com/folder/folder1/index.php?word=blahblah

我希望访问 www.domain.com/folder/folder1/blahblah 的用户在不更改 URL 的情况下重定向到上述 URL。

因此,我在文件夹/folder1/ 中有以下 .htaccess,它运行良好:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)$ index.php?word=$1

所以我想用 nginx 实现相同的功能,我使用了两个转换器: http://www.anilcetin.com/convert-apache-htaccess-to-nginx/ 结果:

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 ^/(.+)$ /index.php?word=$1;
}

http://winginx.com/htaccess 结果:

 if (!-e $request_filename){ rewrite ^(.+)$ /index.php?word=$1; }

现在,我尝试了两种方法,但都不起作用。我尝试将它们插入

location / {
}

或在

location /folder/folder1 {
}

或在

location ~ \.php$ {
}

在所有位置我都收到 404 错误

nginx 错误报告“从上游读取响应标头时主脚本未知”或“没有这样的文件或目录”。

有人可以告诉我吗?

提前致谢!

【问题讨论】:

  • rewrite ^(.*(?![\.js|\.css])[^.]+)$ /index.php/$1 last; 重写所有内容,只忽略 js 和 css 文件...我是带着这个问题来到这里的。

标签: url-rewriting nginx rewrite


【解决方案1】:

首先,不要使用if。如果是邪恶的 -> http://wiki.nginx.org/IfIsEvil

您可以使用以下重写规则来完成此操作。

rewrite ^/folder/folder1/(.*)$ /folder/folder1/index.php?word=$1 last;

将此重写规则放在您的正上方location / {}

【讨论】:

  • 这类作品。它有两个问题:a)它没有避免调用/folder/folder1/existing_file.txt的问题b)css搞砸了,根本没有css
猜你喜欢
  • 2021-01-06
  • 2020-04-08
  • 1970-01-01
  • 2015-03-19
  • 2014-02-18
  • 2022-08-19
  • 2015-09-12
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多