使用 .htaccess 重定向 IP 的条件语句?

发布于 2024-09-10 13:30:54 字数 433 浏览 3 评论 0原文

大家好,我正在托管自己的服务器,并且有两个 IP 地址(一个用于从内部网内部连接的人,一个用于从外部连接的人),并且我有两个动态 dns 重定向到这些 IP。我刚刚在基于 apache2 的服务器上安装/设置了 SSL,并尝试使用 .htaccess 强制用户使用 https。我使用这样的语句使其工作。

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$https://mywebsite.dyn.site/$1 [R,L]

问题是,由于我对同一网站有两个单独的域名,因此该语句会将所有流量重定向到一个特定的域。我想我要问的是是否有a.)一种方法来放置条件if语句来检查他们的ip,或者最好b.)一种方法来删除“mywebsite”的硬编码,并为无论用户输入什么。

任何帮助将不胜感激。

Hey guys, I am hosting my own server and have two ip addresses for it (one for people connecting from within intranet and one for people connecting from outside) and I have two dynamic dns's redirecting to these ip's. I have just installed/setup SSL on my apache2-based server, and am trying to use .htaccess to force users to use https. I got it working using a statement like

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$https://mywebsite.dyn.site/$1 [R,L]

The problem is that since I have two separate domain names for the same website, that statement will redirect all traffic to one specific domain. I guess what I'm asking is whether there is a.) a way to put a conditional if statement to check for their ip, or preferably b.) a way to remove the "mywebsite" from being hardcoded, and putting a variable for whatever the user enters.

Any help would be greatly appreciated.

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

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

发布评论

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

评论(1

皓月长歌 2024-09-17 13:30:54

好吧,我搜索了大约半个小时,然后在我发布到 SO 上后,我找到了答案......哈哈。对于任何尝试做类似事情的人来说,您所要做的就是使用网址变量,而不是对其进行硬编码。

我这样做的方法是将其添加到我的 .htaccess 中:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

这样它只会接受输入的任何 url 并将其从 http 更改为 https

干杯

Well I searched for about half an hour, and then right after I posted on SO I found the answer...lol. For anyone who is trying to do a similar thing all you have to do is use a variable for the web address, instead of hard coding it.

The way I did it was by adding this to my .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

This way it simply will take any url entered and change it from http to https

Cheers

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