.htaccess mod_rewrite - 阻止symfony登录“/(admin | login)”

时间:2015-12-01 10:47:20

标签: apache .htaccess symfony mod-rewrite

我想仅允许从某些IP地址(两个192.168.0.1,10.10.10.1)访问/ admin和/ login。我已经学会here我必须使用像RewriteCond %{REMOTE_ADDR} !^192\.168\..*$这样的东西,但在我的例子中如何更精确地做到这一点?

我使用Symfony2并在我的/ www文件夹中有一个非常基本的htaccess文件。

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} calculator.ipsum.de$
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteCond %{REQUEST_URI} !\.
    RewriteRule (.*) index.php [L]
</IfModule>

2 个答案:

答案 0 :(得分:1)

您可以尝试:

RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.1$ [OR]
RewriteCond %{REMOTE_ADDR} !^10\.10\.10\.1$
RewriteRule (admin|login)/?$ - [F]

这应该允许403 - 禁止尝试从条件以外的IP访问该页面。

答案 1 :(得分:0)

您可以使用Symfony的access control代替。因此,在app/config/security.yml中,添加:

security:
    access_control:
        - { path: ^/admin, roles: ROLE_USER_IP, ips: [192.168.0.1, 10.10.10.1] }
        - { path: ^/login, roles: ROLE_USER_IP, ips: [192.168.0.1, 10.10.10.1] }

这应该允许192.168.0.110.10.10.1访问这些页面,同时阻止其他任何内容。

或者,如果您想在服务器上而不是在应用程序中执行此操作,则可以使用Apache的mod_access module而不是重写:

<Location "/admin">
    Order deny,allow
    Allow from 192.168.0.1 10.10.10.1
    Deny from All
</Location>

<Location "/login">
    Order deny,allow
    Allow from 192.168.0.1 10.10.10.1
    Deny from All
</Location>