Apache 重写规则 - 防止重写的 URL 出现在浏览器 URL 栏中 [英] Apache rewrite rule - prevent rewritten URL appearing in browser URL bar

查看:29
本文介绍了Apache 重写规则 - 防止重写的 URL 出现在浏览器 URL 栏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在寻找特定 URI 的重写规则.当它匹配特定的 URL 时,它会用正确的文件路径重写它,以便可以找到所需的内容.然后它将协议更改为 HTTPS 并允许请求通过.

I have a rewrite rule which is looking for a particular URI. When it matches the particular URL it rewrites it with a proper file path so the required content can be found. It then changes the protocol to HTTPS and allows the request to pass through.

我有两个问题;

  1. 我不希望重写的路径出现在用户浏览器中 - 我想维护虚 url
  2. 我确实希望 HTTPS 协议出现,向用户表明他们正在通过安全连接访问该站点.

我尝试了几种选择,但都没有成功.如果我包含 [R] 标志,则 URL 和协议保持不变,但这不是预期的效果

I have tried a couple of options but no success. If i include the [R] flag the URL and protocol remain unchanged but that is not the desired effect

关于如何实现这一目标有什么建议吗?

Any suggestions on how i can achieve this?

这是我的规则;

RewriteMap redirectsIfSecure txt:/myserver/content/secure_urls.txt
RewriteCond ${lowercase:%{REQUEST_URI}} ^/(.+)$
RewriteCond ${redirectsIfSecure:%1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ https://myserver.com${redirectsIfSecure:%1} [PT]

推荐答案

来自 mod_rewrite 文档:

From the mod_rewrite documentation:

如果指定了绝对 URL,mod_rewrite 会检查是否主机名与当前主机匹配.如果是,则方案和主机名被剥离,结果路径被视为 URL 路径.否则,将对给定 URL 执行外部重定向.到强制外部重定向回当前主机,请参阅 [R] 标志下面.

If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

如果您将请求重写为完全限定的 URL(即以 http://https:// 等开头的任何内容),则不会匹配您的 ServerName,然后 mod_rewrite 将发出 HTTP 重定向,这将导致客户端浏览器从新位置请求资源.

If you rewrite the request to a fully qualified URL (that is, anything starting with http://, https://, etc) that doesn't match your ServerName, then mod_rewrite will issue an HTTP redirect, which will cause the client browser to request the resource from the new location.

如果您不想在 http 和 https 之间切换,您可以使用代理规则(P 标志)让 Apache 代表客户端发出请求并返回结果,因此屏蔽重写的 URL.

If you're not trying to switch between http and https you can use a proxy rule (the P flag) to have Apache make the request on behalf of the client and return the result, thus masking the rewritten URL.

但是,如果您尝试从 http 升级到 https(或其他方式),这将总是需要客户端重定向.

However, if you're trying to upgrade from http to https (or the other way around), this will always require a client redirect.

这篇关于Apache 重写规则 - 防止重写的 URL 出现在浏览器 URL 栏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆