简体   繁体   English

与mod_rewrite斗争

[英]Struggle with mod_rewrite

I am struggling with mod_rewrite htaccess for at least couple of days, and still cannot figure this out. 我在至少几天里一直在用mod_rewrite htaccess挣扎,但仍然无法解决。

I want to force HTTPS SSL on my site, but only from outside of the network. 我想在我的网站上强制使用HTTPS SSL,但只能从网络外部进行。

I have something like this: 我有这样的事情:

RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.30
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mysite.com/$1 [R,L]

My local IP is 192.168.1.30 and it keeps correcting my adress to https://www.mysite.com . 我的本地IP是192.168.1.30 ,它不断将我的地址更正为https://www.mysite.com In one condition it allows me to connect locally to my server. 在一种情况下,它允许我在本地连接到服务器。 When I type https://192.168.1.10 (my local server adress). 当我输入https://192.168.1.10 (我的本地服务器地址)。 But it keeps throwing me SSL caution which cannot be kept this way. 但是它总是让我对SSL保持谨慎,这不能用这种方式保持。

When I type http://192.168.1.10 it redirects me to https://www.mysite.com 当我输入http://192.168.1.10它会将我重定向到https://www.mysite.com

How to make it leave my ip alone from all the redirects? 如何使所有重定向都离开我的IP?

For my logic, it should not redirect me no matter what if my REMOTE_ADDR is 192.168.1.30 . 按照我的逻辑,无论我的REMOTE_ADDR是否为192.168.1.30 ,它都不应重定向我。

Can you try this rule: 您可以尝试以下规则:

RewriteEngine On

RewriteCond %{REMOTE_ADDR} !^(192\.168\.|127\.0\.0\.1)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

I do not think you need Rewrite rules at all. 我认为您根本不需要重写规则。 Since HTTP listens on port 80 and HTTPS on port 443, you can have three different VirtualHosts. 由于HTTP侦听端口80和HTTPS侦听端口443,因此您可以拥有三个不同的VirtualHost。

  1. First one to listen on port 80 and binds to your private ip address. 第一个监听端口80并绑定到您的私有IP地址。
  2. Second to listen on port 80 but all it does in 301 redirect to the https url 第二个监听端口80,但它在301中所做的所有操作都重定向到https url
  3. The last one to listen on port 443 (the HTTPS) 最后一个监听端口443(HTTPS)

However, you may have to move this logic from htaccess file to your .conf file. 但是,您可能必须将此逻辑从htaccess文件移动到.conf文件。

Since apache starts finding the matching VirtualHost in the order they are defined in the .conf file (http.conf or apache.conf as the case may be), the ordering is very important. 由于apache开始按照在.conf文件中定义的顺序(视情况而定为http.conf或apache.conf)查找匹配的VirtualHost,因此顺序非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM