【问题标题】:How to prevent RewriteRule in local network? (not localhost)如何防止本地网络中的 RewriteRule? (不是本地主机)
【发布时间】:2022-01-16 16:32:28
【问题描述】:

当连接到本地网络上的 MacBook 时如何防止 RewriteRule?

我正在使用 MAMP,并希望通过 iPhone 上的 MacBook 访问网站。它适用于:

http://my-macbook.local:8888

问题是我的 .htaccess 文件中有重写规则以将 http 重定向到 https:

RewriteEngine on

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

如您所见,我有一个 !localhost 条件,如果我只是通过 localhost 连接它会很好,但是当我尝试 my-macbook.local:8888 时它会重定向到网站。

在搜索了类似问题后,我到目前为止尝试过的事情:

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

以上都不起作用。

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您可以将此规则与附加条件一起使用:

    # 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]
    

    【讨论】:

    • 非常感谢! :) 添加RewriteCond %{SERVER_PORT} !=8888 解决了我的问题
    猜你喜欢
    • 2019-02-05
    • 2023-03-04
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    相关资源
    最近更新 更多