Flash
Flash

Reputation: 476

Struggle with mod_rewrite

I am struggling with mod_rewrite htaccess for at least couple of days, and still cannot figure this out.

I want to force HTTPS SSL on my site, but only from outside of the network.

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. In one condition it allows me to connect locally to my server. When I type https://192.168.1.10 (my local server adress). But it keeps throwing me SSL caution which cannot be kept this way.

When I type http://192.168.1.10 it redirects me to https://www.mysite.com

How to make it leave my ip alone from all the redirects?

For my logic, it should not redirect me no matter what if my REMOTE_ADDR is 192.168.1.30.

Upvotes: 1

Views: 139

Answers (2)

anubhava
anubhava

Reputation: 785128

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]

Upvotes: 1

Tippa Raj
Tippa Raj

Reputation: 594

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.

  1. First one to listen on port 80 and binds to your private ip address.
  2. Second to listen on port 80 but all it does in 301 redirect to the https url
  3. The last one to listen on port 443 (the HTTPS)

However, you may have to move this logic from htaccess file to your .conf file.

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.

Upvotes: 0

Related Questions