Nginx 的规范 URL

发布于 2024-12-05 17:48:40 字数 383 浏览 3 评论 0原文

我们正在努力从 URL 中删除目录索引文件,以进行清理并提供更高的一致性,从而改进我们的 SEO。

但是,我不熟悉如何在 Nginx 中处理这个问题。

我找到了 Apache 的以下内容(我们只是在寻找 Nginx 的等效项)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/ 
RewriteRule     ^(([^/]+/)*)index\.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]  

我已经阅读了文档并尝试了几种不同的选项 - 我能得到的最接近的选项仍然会返回无限循环错误。

We're working on removing the directory index files from our URLs to clean things up and provide more consistency to improve our SEO.

However, I'm not familiar with how to take care of this in Nginx.

I found the following for Apache (we're just looking for the Nginx equivalent)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/ 
RewriteRule     ^(([^/]+/)*)index\.php$ http://www.%{HTTP_HOST}/ [R=301,NS,L]  

I've read the docs and tried several different options - the closest I can get will still return an infinite loop error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-12-12 17:48:40

您为 Apache 发布的代码片段使用不可变的全局变量 %{THE_REQUEST} 来确定客户端请求的原始 URI。但是,此变量包含整个请求,包括 HTTP 方法、版本和查询字符串。因此,解析这个变量有点混乱,如您发布的示例所示。

但是,nginx 有一个专用变量,用于保存从客户端接收到的原始请求 URI:$request_uri。这允许您执行以下操作:

## REDIRECT foo/index(.html) to foo/
if ($request_uri ~ ^(.*/)index(?:\.html)?$) {
    return 301 $1;
}

如果您还想删除文件后缀,例如 .html,您可以使用以下代码片段:

## REDIRECT foo/bar.html to foo/bar
if ($request_uri ~ ^(.+)\.html$) {
    return 301  $1;
}

现在,为了让 nginx仍然能够提供正确的文件,可以使用 try_files 指令,该指令按顺序检查所有给定的 URI,直到有一个匹配为止:

## Rewrite internal requests for foo/bar to foo/bar.html
try_files $uri $uri.html =404;

因此,对 /foo/bar 的请求将是处理如下:

  1. 返回$uri = /foo/bar,如果该文件存在于文档中
    root,否则
  2. 返回 $uri.html = /foo/bar.html 如果存在,最后
  3. 发出 404 错误。

The snippet you posted for Apache uses the immutable global variable %{THE_REQUEST} to determine the original URI requested by the client. However, this variable contains the entire request, including the HTTP method, version, and query string. Therefore, parsing this variable is a bit messy, as seen in the example you posted.

However, nginx has a dedicated variable that holds the original request URI received from the client: $request_uri. This allows you to do the following:

## REDIRECT foo/index(.html) to foo/
if ($request_uri ~ ^(.*/)index(?:\.html)?$) {
    return 301 $1;
}

If you wanted to also strip the file suffix, e.g. .html, you could use the following snippet:

## REDIRECT foo/bar.html to foo/bar
if ($request_uri ~ ^(.+)\.html$) {
    return 301  $1;
}

Now, in order for nginx to still be able to serve the correct file, one uses the try_files directive, which checks all given URIs in sequence until one matches:

## Rewrite internal requests for foo/bar to foo/bar.html
try_files $uri $uri.html =404;

So a request for /foo/bar would be handled as follows:

  1. return $uri = /foo/bar, if that file exists in the document
    root, otherwise
  2. return $uri.html = /foo/bar.html if it exists, finally
  3. issue a 404 error.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文