Nginx 的规范 URL
我们正在努力从 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为 Apache 发布的代码片段使用不可变的全局变量
%{THE_REQUEST}
来确定客户端请求的原始 URI。但是,此变量包含整个请求,包括 HTTP 方法、版本和查询字符串。因此,解析这个变量有点混乱,如您发布的示例所示。但是,
nginx
有一个专用变量,用于保存从客户端接收到的原始请求 URI:$request_uri
。这允许您执行以下操作:如果您还想删除文件后缀,例如
.html
,您可以使用以下代码片段:现在,为了让
nginx
仍然能够提供正确的文件,可以使用try_files
指令,该指令按顺序检查所有给定的 URI,直到有一个匹配为止:因此,对
/foo/bar
的请求将是处理如下:$uri
=/foo/bar
,如果该文件存在于文档中root,否则
$uri.html
=/foo/bar.html
如果存在,最后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:If you wanted to also strip the file suffix, e.g.
.html
, you could use the following snippet:Now, in order for
nginx
to still be able to serve the correct file, one uses thetry_files
directive, which checks all given URIs in sequence until one matches:So a request for
/foo/bar
would be handled as follows:$uri
=/foo/bar
, if that file exists in the documentroot, otherwise
$uri.html
=/foo/bar.html
if it exists, finally