php - htaccess https 在父目录中,但在子目录中为 http

标签 php apache .htaccess http redirect

我已尝试针对此问题实现一些建议的解决方案,但无法解决任何问题。

我正在使用 2 个 .htaccess 文件,其中一个在我的根目录中,内容如下:

RewriteEngine on
RewriteBase /
RewriteRule ^(webservice|api) - [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.domain.com%{REQUEST_URI}  [R=301,L]

这会通过我需要的 https 将所有流量重定向到主网站。

但是在子目录中,我想禁用重新路由并强制所有通过 http 进入该目录的请求。/webservice/rest/中的 .htaccess 文件内容如下:

RewriteEngine On
RewriteBase /webservice/rest/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.domain.com/webservice/rest/%{REQUEST_URI}  [R=301,L]
RewriteRule ^testingPost/?$ testingPost.php [NC]
RewriteRule ^([a-zA-Z0-9\_\-]+)/?(\?([a-zA-Z0-9\_\-\=\?\&]))?$ otherfiledirect.php?method=$1&params=$2 [NC]

发生了一些重定向,但是第二个 htaccess 中的第 3 行和第 4 行似乎没有做任何事情 - 谁能看到我做错了什么以及如何在主站点上保留 https 但强制http/webservice/rest/下的任何 url ??

干杯

最佳答案

问题出在服务器中的 ssl 配置文件上。允许覆盖需要在端口 443 的安全虚拟主机下设置为全部。更改后,根 htaccess 文件中的以下代码允许我在除特定文件夹之外的整个网站上强制使用 https:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/webservice     [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L]

因此任何对 http 的请求都会转到 https。但是我仍然需要在我指定的文件夹上明确地将请求更改为 https 以重定向到 http,因此在上面添加了以下代码:

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} /webservice      [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

子弹证明。

关于php - htaccess https 在父目录中,但在子目录中为 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647300/

相关文章:

php - 如何在不使用 php 拇指的情况下获取多种尺寸的图像?

javascript - Bootstrap 3 中的链式选择?

java - apache HttpClient,基于表单的登录,并检索 HTML 内容

linux - 如何在 Docker 容器内的 httpd alpine 中使用 https 运行本地主机?

regex - HTACCESS重写: Ignore another rewrite rule

php - 将主数据库复制到不同的从数据库

t.me 链接的 php 正则表达式

apache - SSL认证apache

wordpress - 解析 header X-XSS-Protection : 1; Over SSL 时出错

根目录中的 Wordpress htaccess 覆盖子域中的 htaccess。子域应用程序现在不工作