url-rewriting - nginx php文件重写url

标签 url-rewriting nginx rewrite

所以使用 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 错误报告“从上游读取响应头时未知主脚本”或“没有这样的文件或目录”。

有人可以启发我吗?

提前致谢!

最佳答案

一、不要用if .如果是邪恶的 -> http://wiki.nginx.org/IfIsEvil

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

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

将此重写规则放在您上方 location / {}堵塞

关于url-rewriting - nginx php文件重写url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580066/

相关文章:

php - php和mysql的url重写规则

ubuntu - 我现在由 NginX 提供服务的网站的正确权限是什么?

nginx重写规则不起作用?

regex - 如何通过 .htaccess 删除 URL 中任意位置的空参数或参数?

IIS URL 重定向更改查询字符串参数名称

nginx - 如何使Nginx虚拟主机工作? (当前显示403禁止错误)

error-handling - 在Nginx中,它必须是自动索引或目录禁止错误?

php - 如何配置 nginx 重写规则以使 CakePHP 在 CentOS 上运行?

php - .htaccess 替换(重写)PHP URL 中的 $_GET 变量

asp.net - 调用 HttpContext.RewritePath() 后如何获取原始 url