.htaccess例外规则不起作用

时间:2017-06-05 06:22:03

标签: php apache .htaccess

这些是.htaccess中的当前规则。基本上,我想将我的整个网站从domain1.com重定向到domain2.com,除了以下条件:

  1. /offers文件夹

  2. 内的所有文件/页面
  3. /page/company-a

  4. 页面

    当我访问www.domain1.com/page/company-a时,我被重定向到domain2.com/index.php

    任何建议表示赞赏。

    RewriteCond %{REQUEST_URI} !^/offers
    RewriteCond %{REQUEST_URI} !^/page/company-a
    
    RewriteCond %{HTTP_HOST}  ^www\.domain1\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
    
    
    # Pass all requests not referring directly to files in the filesystem to
    # index.php. Clean URLs are handled in drupal_environment_initialize().
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    RewriteRule ^ index.php [L]
    

1 个答案:

答案 0 :(得分:2)

使用THE_REQUEST变量代替REQUEST_URITHE_REQUEST变量表示Apache从您的浏览器收到的原始请求,并且在执行一些重写规则后不会被覆盖。

RewriteEngine On

RewriteCond %{THE_REQUEST} !\s/+(offers|page/company-a) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain1\.com$ [NC]
RewriteRule ^ http://www.domain2.com%{REQUEST_URI} [L,R=301,NE]

# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

确保在测试时清除浏览器缓存。