Home > OS >  How to prevent RewriteRule in local network? (not localhost)
How to prevent RewriteRule in local network? (not localhost)

Time:12-13

How to prevent RewriteRule when connected to MacBook on local network?

I am using MAMP and want to access website from my MacBook on my iPhone. It works great with:

http://my-macbook.local:8888

The problem is I have rewrite rule in my .htaccess file to redirect http to https:

RewriteEngine on

#redirect to https
RewriteCond %{HTTP_HOST} !localhost
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www\.mywebsite\.com/%{REQUEST_URI} [L,R=301]

As you can see, I have a !localhost condition and it works great if I just connect through localhost, but it redirects to website when I'm trying my-macbook.local:8888.

Things I've tried so far after searching similar questions:

RewriteCond %{HTTP_HOST} !http://my-macbook.local:8888
RewriteCond %{HTTP_HOST} !^my-macbook$
RewriteCond %{REQUEST_URI} !my-macbook

Nothing above works.

CodePudding user response:

You can use this rule with an additional condition:

# redirect to https if 1. port is NOT 8888 and 2. client is not local
RewriteCond %{SERVER_PORT} !=8888
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
  • Related