php - .htaccess 异常规则不起作用

标签 php apache .htaccess

这些是我在 .htaccess 中的当前规则。基本上,我想将我的整个站点从 domain1.com 重定向到 domain2.com,但以下情况除外:

  1. /offers 文件夹中的所有文件/页面

  2. /page/company-a 页面

当我访问 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]

最佳答案

使用 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]

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

关于php - .htaccess 异常规则不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44362837/

相关文章:

wordpress - 基于浏览器 SNI 支持的重定向

php - 这里我的 SQL 语法有什么问题?

php - 使用 CENTOS 在 PHP 上运行终端命令

php - 何时使用 : redirect ('/' ) vs. redirect()->route ('home' ) vs. redirect()->home()?

apache - .htaccess页面和lang错误

apache - socket.io 与 Apache Tomcat

php - 使用 htaccess 使 url 不区分大小写

apache - .htaccess 在不是 www 时强制 ssl

javascript - 创建特定格式的 JSON 数据

php - curl_errno 返回 0 而不是 6