nginx php文件重写网址

时间:2013-01-29 09:55:56

标签: url-rewriting nginx rewrite

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

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

我希望访问www.domain.com/folder/folder1/blahblah的用户重定向到上面的网址,而不会更改网址。

因此我在文件夹/ 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错误报告"主要脚本未知,同时从上游读取响应标头"或者"没有这样的文件或目录"。

有人可以开导我吗?

提前感谢!

1 个答案:

答案 0 :(得分:7)

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

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

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

将此重写规则置于location / {}阻止

之上