如何链接重写在Nginx中?

我是nginx重写引擎的新手。 我试图将我的旧的htaccess文件转换为nginx的格式,但我遇到了一些麻烦。

# ------------------------------------------------------ # # This redirects index.php to / # # ------------------------------------------------------ # RewriteCond %{THE_REQUEST} ^[AZ]+\ /(index|index\.php)\ HTTP/ RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] # ------------------------------------------------------ # # This rewrites 'directories' to their PHP files, # # fixes trailing-slash issues, and redirects .php # # to 'directory' to avoid duplicate content. # # ------------------------------------------------------ # RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.*)$ $1.php [L] RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.*)/$ http://twitstamp.com/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[AZ]+\ /[^.]+\.php\ HTTP/ RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^.]+)\.php$ http://twitstamp.com/$1 [R=301,L] # ------------------------------------------------------ # # If it wasn't redirected previously and is not # # a file on the server, rewrite to image # # ------------------------------------------------------ # RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=${escapemap:$1}&template=${escapemap:$2} [NC,L] 

这是我的htaccess文件。 现在,这是我到目前为止…

 # ------------------------------------------------------ # # This redirects index.php to / # # ------------------------------------------------------ # if ($request_uri ~* "^/index.php\??$") { rewrite ^.*$ http://$host? permanent; } # ------------------------------------------------------ # # This rewrites 'directories' to their PHP files, # # fixes trailing-slash issues, and redirects .php # # to 'directory' to avoid duplicate content. # # ------------------------------------------------------ # if (!-e $request_filename) { rewrite ^(.*)$ $1.php; last; } # ------------------------------------------------------ # # If it wasn't redirected previously and is not # # a file on the server, rewrite to image # # ------------------------------------------------------ # if (!-e $request_filename) { rewrite ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=$1&template=$2; break; } 

index.phpredirect工作正常,“目录”名称 – > PHP文件redirect。 然而,我不知道如何做几件事情:实施尾随斜线修复,外部redirect.php文件,以便我没有任何重复的文件。 我希望所有页面都看起来干净,比如/ help,/ about等。服务器上的真实页面是/about.php格式。 另外,我无法得到工作的图像的重写规则。 我想要的东西不是一个真正的文件或目录(-e标志),而不是一个可重写的文件(如/约)redirect到一代/等…

你错了。 Nginx喜欢位置块,例如将/index.phpredirect到/你会做。

 location = /index.php { rewrite ^ http://domain.com/$args permanent; } 

你也想看看try_files。 一般来说,如果你在Nginx中使用if,那么你很可能做错了。

我build议阅读这个关于Nginx如何使用服务器块和位置块的基本介绍: http : //blog.martinfjordvald.com/2010/07/nginx-primer/