所以使用 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/