【问题标题】:How to make complex conditions in mod_rewrite?如何在 mod_rewrite 中制作复杂的条件?
【发布时间】:2012-01-08 05:47:30
【问题描述】:

我需要拒绝除某些 IP 之外的所有人访问整个网站。 另外,我需要允许每个人访问站点的一个文件夹:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on

# Allow access only for devs
RewriteCond %{REMOTE_ADDR} !10.10.10.10 [NC] # First dev id
RewriteCond %{REMOTE_ADDR} !11.11.11.11 [NC] # Second dev id

# Allow direct access to files
RewriteCond %{REQUEST_FILENAME} !-f

# Redirecting guests
RewriteRule (.*) /coming/soon/index.html [R=307]

# But where to place this condition?
RewriteRule ^/?preview/?$ /preview/index.html [NC]


# Other rules for main site structure
# ...

所以,我只需要为开发人员加载整个网站。其他用户(客人)将看到/coming/soon/ 页面 并且还允许访客查看网站的/preview/ 页面。

如何做到这一点?

【问题讨论】:

    标签: mod-rewrite conditional-statements


    【解决方案1】:

    如果你的/preview/重写适合所有用户并且不依赖后续的重写规则,最简单的方法是把这个RewriteRule放在前面加上[L]标志,这样后续的重写就不会应用了。

    否则,RewriteRule 的异常可能被指定为 RewriteCond%{REQUEST_URI} 匹配:

    RewriteCond %{REQUEST_URI} !^/?preview/?$ [NC]
    

    另请注意,您建议的规则会将/preview/preview/ 重写为/preview/index.html,并且这些重写中的第一个可能会破坏相关链接,除非执行重定向。

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多