如何在 Nginx 中链接重写?

如何在 Nginx 中链接重写?

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

# ------------------------------------------------------ #
# This redirects index.php to /                          #
# ------------------------------------------------------ #

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(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} ^[A-Z]+\ /[^.]+\.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.php 重定向工作正常,"目录"名称 -> php 文件重定向也是如此。但是,我不知道如何做几件事:实现尾部斜杠修复,以及外部重定向 .php 文件,这样我就没有任何重复的文件。我希望所有页面看起来都很干净,例如 /help、/about 等。服务器上的实际页面是 /about.php 格式。此外,我无法让图像的重写规则正常工作。我希望任何不是真实文件或目录(-e 标志)且不是可重写文件(如 /about)的内容重定向到generation/etc...

答案1

你的做法是错误的。Nginx 喜欢位置块,例如将 /index.php 重定向到 / 即可。

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

您还需要查看 try_files。一般来说,如果您在 Nginx 中使用 if,则很有可能您做错了。

我建议阅读此文,以了解 Nginx 如何使用服务器块和位置块的基本介绍:http://blog.martinfjordvald.com/2010/07/nginx-primer/

相关内容