Mod Rewrite限制访问

时间:2014-08-01 09:53:12

标签: php apache .htaccess mod-rewrite

使用Zend Skeleten应用程序并在.htaccess中具有以下规则。

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

尝试将加速仅限制为4个特定网址

  1. /(索引页)
  2. /login
  3. /register
  4. /auth(授权页面)
  5. 添加

    RewriteCond %{REQUEST_URI} !register$
    RewriteCond %{REQUEST_URI} !login$
    RewriteCond %{REQUEST_URI} !index$
    RewriteCond %{REQUEST_URI} !auth$
    RewriteRule ^(.+)$ /chat/ [NC]
    

    但这似乎不起作用。

    • 索引文件localhost/testlocalhost/test/index
    • 的完整路径
    • 登录文件localhost/test/login
    • 的完整路径
    • 注册文件localhost/test/register
    • 的完整路径
    • authfile localhost/test/auth
    • 的完整路径

    问题

    localhost / test /与localhost / test / index和localhost / test / index / index相同 为localhost / test / login保存,与localhost / test / login / index相同。试图在.htaccess中实现这一点。但它似乎没有工作[OR]条件。

    RewriteCond %{REQUEST_URI} !^/test/(register|login|index|auth)$ [OR]
    RewriteCond %{REQUEST_URI} !^/test/(register|login|index|auth)/(index)$ [OR] **edited**
    RewriteCond %{REQUEST_URI} !^/test/$ 
    RewriteRule ^ - [F]
    

    已解决的问题

    RewriteCond %{REQUEST_URI} !^/test/(\/|register|register/index|login|login/index|index|index/index|auth|auth/index|)$
    RewriteRule ^ - [F]
    

1 个答案:

答案 0 :(得分:1)

您可以拥有此规则:

RewriteCond %{REQUEST_URI} !(register|login|index|auth)$
RewriteRule ^ - [F]