https后面的Apache目录尾部斜杠问题

编程入门 行业动态 更新时间:2024-10-24 08:19:44
本文介绍了https后面的Apache目录尾部斜杠问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在https门户后面运行了一个apache服务器,目录重定向出现了问题. 我的apache服务器是一个docker容器,它接收由https门户容器(steveltn/https-portal)转发的请求. 一切正常,除了对http而不是https进行的默认http重定向之外.例如,假设我们在我的apache网站中有一个名为test的目录.调用 example/test 会返回301代码,并重定向到 example/test/. 正确的做法是重定向到https.

I run an apache server behind an https portal and have an issue with directory redirection. My apache server is a docker container that receives requests forwarded by an https portal container (steveltn/https-portal). Everything works fine, except the default http redirections that are made to http instead of https. For instance, let say we have a directory named test in my apache website. Calling example/test returns a 301 code with redirection to example/test/. The right behaviour would be to have a redirection to https.

我首先认为这是我的https门户的配置错误,然后问steveltn/https-portal团队.但是他们回答说这是我的apache配置中的一个问题( github /SteveLTN/https-portal/issues/67#issuecomment-257934618 ). 答案的摘要是

I first thought it was a misconfiguration of my https portal, and asked steveltn/https-portal team. But they replied it's a problem in my apache configuration (github/SteveLTN/https-portal/issues/67#issuecomment-257934618). The summary of the answer is

PORTAL确实通过请求标头告知Apache它的存在 X-Forwarded-Proto:https.某些网络应用可以识别此标头 自动,例如WordPress.我想现在由您决定 配置您的Web应用程序以识别此标头

PORTAL does tell the Apache about its existence by the request header X-Forwarded-Proto: https. Some web apps recognize this header automatically, such as WordPress. I guess now it up to you to configure your web app to recognize this header

例如,我尝试了许多在Internet上找到的配置,但是没有一个解决问题的方法:

I tried a lot of configuration found on the Internet for instance this one, but none fixes the problem:

<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) %{HTTP_HOST}%{REQUEST_URI} [R,L] </VirtualHost>

如果将RewriteCond更改为%{HTTP:X-Forwarded-Proto} https,则会出现无限重定向循环.

If I change the RewriteCond to %{HTTP:X-Forwarded-Proto} https, I get a infinite redirection loop.

有什么主意吗?

推荐答案

在处理Gerfried提案之后,这里是我使用的解决方案.

After working on Gerfried proposal here is the solution I use.

RewriteEngine On RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} -d RewriteCond %{REQUEST_URI} !(.*)/$ RewriteCond %{HTTP:X-Forwarded-Proto} https RewriteRule ^(.*)$ %{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]

它在以下情况下有效:

  • 请求目录-> https重定向
  • 对现有文件的请求 ->提供文件
  • 请求缺少资源-> http 404错误.
  • Request to a directory -> https redirect
  • Request to an existing file -> serves the file
  • Request to a missing resource -> http 404 error.

更多推荐

https后面的Apache目录尾部斜杠问题

本文发布于:2023-11-01 23:08:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   尾部   目录   https   Apache

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!