【问题标题】:.htaccess redirect to all IP's but mine.htaccess 重定向到所有 IP,但我的
【发布时间】:2012-02-17 14:54:28
【问题描述】:

基本上,我正在尝试在网站的前端工作,但如果您愿意,我希望除我自己以外的其他人都被重定向到构建页面。我目前有:

redirect 301 /index.php http://www.domain.com/construction.php

虽然这可行,但效果很好,我希望自己仍然能够看到实时站点,是否可以排除除我的 IP 之外的所有人?

再次感谢。

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    你可以用 mod_rewrite 做到这一点

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REMOTE_ADDR} !=123.45.67.89
    RewriteRule index.php$ /construction.php [R=301,L]
    

    【讨论】:

    • 添加 IP 怎么样,比如 IP 列表?
    • @BenRacicot:试试这样的:stackoverflow.com/questions/11653461/…
    • Construction.php 上的背景图片怎么样?那也会被重定向,如何解决?
    • 2 注意:这只会将请求重定向到“index.php”而不是任何其他文件,并且您可能还需要包含不重定向您在构建页面中引用的任何文件的条件。因此@linuskleen 的回答效果更好stackoverflow.com/a/8985628/5441
    【解决方案2】:

    在重定向之前你需要一些条件:

    RewriteCond %{REMOTE_ADDR} !=1.3.3.7
    RewriteCond %{REQUEST_URI} !=/construction.php
    RewriteRule .* /construction.php [L]
    

    另外,为了确保在解除锁定后,客户端将看到实际页面,此解决方案不会永久地重定向客户端(使用 301 重定向),而是在内部重定向。将 1.3.3.7 替换为您正在使用的实际 IP 地址。

    【讨论】:

      【解决方案3】:

      如果您的 apache 版本是 2.4* ,您可以使用 htaccess 中的以下指令将访问者重定向到构建页面:

      <If "%{REMOTE_ADDR} !='yourIp'">
      RedirectMatch ^/((?!construction.php).*)$ /construction.php
      </If>
      

      如果ip地址不是yourIp重定向所有请求到/construction.php

      在旧版本的 apache 上,您可以使用以下基于 mod-rewrite 的解决方案:

      RewriteEngine on
      
      RewriteCond %{REMOTE_ADDR} !^myIP$
      RewriteRule !construction\.php /construction.php [L]
      

      如果 RewriteCondition 满足,这会在内部将请求转发到 /construction.php。如果您想在浏览器地址栏中看到重定向的 url,可以将 L 替换为 R。

      【讨论】:

        【解决方案4】:

        您好,您可以在 .htaccess 文件中执行以下操作

        RewriteEngine on
        # Redirect all except allowed IP
        RewriteCond %{REMOTE_ADDR} !^12.345\.678\.901$
        RewriteRule /index.php http://www.domain.com/construction.php [R=302,L]
        

        用你的 IP 代替 12.345.678.901

        【讨论】:

          【解决方案5】:

          如果您有一系列 IP 想要排除在“建设中”页面之外,您可以使用 |

          RewriteEngine on
          RewriteCond %{REMOTE_ADDR} !^127.0.0.1|212.250.141.228
          RewriteRule ! construction\.html /construction.html [R]
          

          将最后两行放在.htaccess 文件的末尾很重要,尤其是当它包含更多重写规则时。

          【讨论】:

            【解决方案6】:

            以下对我有用

            Deny from all
            Allow from xxx.xxx.xx.xxx 
            

            【讨论】:

            • 这只会拒绝访问,不会按照用户的要求重定向客户端。
            【解决方案7】:

            如果您对在您的 construction.php 中引用背景图像感兴趣,下面的代码可以避免图像被重定向:

            RewriteCond %{REMOTE_ADDR} !=THE_IP
            RewriteCond %{REQUEST_URI} !^\/construction\.php|\/YOUR_IMAGE\.jpg
            RewriteRule .* /construction.php [R=302,L]
            

            【讨论】:

              【解决方案8】:

              除了按照其他答案的建议使用if 指令外,您还可以通过使用&amp;&amp; 运算符将其他条件包含到一个指令中来添加多个IP:

              <If "%{REMOTE_ADDR} != '127.0.0.1' && %{REMOTE_ADDR} != '192.168.1.1'">
                  RedirectMatch ^/((?!construction.php).*)$ /construction.php
              </If>
              

              在此处查看文档:http://httpd.apache.org/docs/2.4/mod/core.html#if

              【讨论】:

                【解决方案9】:

                另一个想法是只允许访问某个范围

                RewriteEngine on
                RewriteBase /
                # Validator
                SetEnvIf Remote_Addr "^128.30." IsInt
                # Local
                SetEnvIf Remote_Addr "^192\.168" IsInt
                Order allow,deny
                Allow from env=IsInt
                

                【讨论】:

                  【解决方案10】:

                  在我找到自己的解决方案之前,没有人工作

                  代码中的网址:http://www.example.com/index_cons.php

                  示例中的IP地址为:75.85.95.105

                  在最新版本的 Cpanel 上测试。

                  RewriteEngine on
                  RewriteCond %{REMOTE_ADDR} !^75\.85\.95\.105
                  RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
                  RewriteCond %{HTTP_HOST} ^www\.example\.com$
                  RewriteRule ^/?$ "http\:\/\/example\.com\/index_cons\.php" [R=302,L]
                  

                  【讨论】:

                    猜你喜欢
                    • 2012-01-31
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-01-10
                    • 2016-05-13
                    相关资源
                    最近更新 更多