仅使用 mod_alias 通过 .htaccess 将目录的 http 重定向到 https

发布于 2024-10-15 23:21:07 字数 804 浏览 0 评论 0原文

我有一个常见的问题,就是希望将对某个受限访问目录的请求从 http 重定向到 https,以便以安全的方式发送用户的登录凭据。

但是,我的服务器上没有启用 mod_rewrite。我只有 mod_alias。这意味着我必须使用 RedirectMatch 命令。我无法使用使用 RewriteCond 和 RewriteRule 的常用解决方案。

(关于政治的注释:我是一个非常大的组织中的小规模子站点维护者,因此服务器管理员不太可能愿意为我更改服务器配置!)

以下行有效,但形成了无限循环(因为重写的 URL 仍然被初始正则表达式捕获):

RedirectMatch permanent ^/intranet(.*)$ https://example.com/intranet$1

我的一位内部 IT 人员建议我通过将文件移动到具有新名称的新目录(例如 /intranet2/)来避免无限循环。这对我来说似乎很丑陋。人们仍然可能通过访问 http://example.com/intranet2/ 意外/故意恢复到不安全的连接直接地。

然后我尝试了这个,但它不起作用:

RedirectMatch permanent ^http:(.*)/intranet(.*)$ https://example.com/intranet$1

我怀疑它不起作用,因为第一个参数必须是根目录的文件路径,所以它不能以“http:”开头。

那么:有更好的想法如何做到这一点吗?

I have the common problem of wanting to redirect requests for a certain restricted access directory from http to https, so that users' login credentials are sent in a secure way.

However, I do not have mod_rewrite enabled on my server. I only have mod_alias. This means I have to use the RedirectMatch command. I can't use the usual solutions that use RewriteCond and RewriteRule.

(A note on the politics: I am a small-fry subsite maintainer in a very large organisation, so the server admins are unlikely to be willing to change the server config for me!)

The following line works, but forms an infinite loop (because the rewritten URL is still caught by the initial regular expression):

RedirectMatch permanent ^/intranet(.*)$ https://example.com/intranet$1

One of my internal IT guys has suggested I avoid the infinite loop by moving the files to a new directory with a new name (eg /intranet2/). That seems pretty ugly to me. And people could still accidentally/deliberately revert to an insecure connection by visiting http://example.com/intranet2/ directly.

Then I tried this, but it didn't work:

RedirectMatch permanent ^http:(.*)/intranet(.*)$ https://example.com/intranet$1

I suspect it didn't work because the first argument must be a file path from the root directory, so it can't start with "http:".

So: any better ideas how to do this?

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

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

发布评论

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

评论(1

绝情姑娘 2024-10-22 23:21:07

mod_alias 指令适用于请求 URI(URL 中域名后面的内容),它们不是路径或 URL。你无法匹配该方案。

mod_alias directives work on request URIs (the stuff that comes after the domain name in a URL), which are not paths or URLs. You can't match against the scheme.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文