仅使用 mod_alias 通过 .htaccess 将目录的 http 重定向到 https
我有一个常见的问题,就是希望将对某个受限访问目录的请求从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.