Apache RewriteRule子域到具有特定条件的路径 更多详细信息

问题描述

我要在不更改Web浏览器上显示URL的情况下,对已经具有多个重写条件的URL进行重写(从子域到路径)。

我必须在.htaccess或VirtualHost配置文件中处理它。

所有域(主域和子域)都指向同一目录。所有请求都会传递到 index.PHP 文件

CMS框架:Drupal 9

来源网址:

https://SUBDOMAIN.example.com/[PATH]?[ParaMETERS]

目标网址:

https://www.example.com/pages/SUBDOMAIN/[PATH]?[ParaMETERS]

例如:

从:https://ZONE1.example.comhttps://www.example.com/pages/ZONE1

从:https://ZONE1.example.com/PAGE1https://www.example.com/pages/ZONE1/PAGE1

从:https://ZONE1.example.com/PAGE1/SECTION1/ARTICLE1https://www.example.com/pages/ZONE1/PAGE1/SECTION1/ARTICLE1

从:https://ZONE1.example.com/PAGE1?KEY1=VALUE1https://www.example.com/pages/ZONE1/PAGE1?KEY1=VALUE1

等...

当前,我在.htaccess文件中具有以下重写条件。

  RewriteEngine on

  RewriteRule ^ - [E=protossl]
  RewriteCond %{HTTPS} on
  RewriteRule ^ - [E=protossl:s]

  RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

  RewriteRule "/\.|^\.(?!well-kNown/)" - [F]

  RewriteCond %{REQUEST_URI} ^(.*)?/(install.PHP) [OR]
  RewriteCond %{REQUEST_URI} ^(.*)?/(rebuild.PHP)
  RewriteCond %{REQUEST_URI} !core
  RewriteRule ^ %1/core/%2 [L,QSA,R=301]

  RewriteRule ^core/install.PHP core/install.PHP?rewrite=ok [QSA,L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.PHP [L]

  RewriteCond %{REQUEST_URI} !/core/[^/]*\.PHP$
  RewriteCond %{REQUEST_URI} !/core/modules/system/tests/https?.PHP
  RewriteCond %{REQUEST_URI} !/core/modules/statistics/statistics.PHP$
  RewriteRule "^(.+/.*|autoload)\.PHP($|/)" - [F]

我尝试代码放在.htaccess文件的末尾,但没有成功(大多数错误500 !!!)。

#01
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]

#02
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.localhost\.local [NC]
RewriteRule ^ index.PHP?q=/testcontents/%1%{REQUEST_URI} [L]
#and also
RewriteRule ^ testcontents/%1%{REQUEST_URI} [L]
#and also
RewriteRule ^ http://localhost.local/testcontents/%1%{REQUEST_URI} [L]
#and also
RewriteRule ^(.*)$ index.PHP?q=/testcontents/%1%{REQUEST_URI} [L]

更多详细信息

如果这有用的话:

所有请求都传递到 index.PHP 文件

var_dump($ _ SERVER)的一部分;在index.PHP中:

  'REDIRECT_STATUS' => string '200' 
  'protossl' => string '' 
  'HTTP_AUTHORIZATION' => string '' 
  'HTTP_HOST' => string 'test1.localhost.local' 
  'HTTP_DNT' => string '1' 
  'HTTP_CONNECTION' => string 'keep-alive' 
  'HTTP_UPGRADE_INSECURE_REQUESTS' => string '1' 
  'HTTP_CACHE_CONTROL' => string 'max-age=0'
  'SERVER_NAME' => string 'test1.localhost.local' 
  'SERVER_ADDR' => string '172.20.0.5'
  'SERVER_PORT' => string '80'
  'REMOTE_ADDR' => string '172.20.0.1'
  'DOCUMENT_ROOT' => string '/var/www/html/drupal9'
  'REQUEST_SCHEME' => string 'http'
  'CONTEXT_PREFIX' => string '' 
  'CONTEXT_DOCUMENT_ROOT' => string '/var/www/html/drupal9' 
  'SERVER_ADMIN' => string '[no address given]' 
  'SCRIPT_FILENAME' => string '/var/www/html/drupal9/index.PHP' 
  'REMOTE_PORT' => string '40142' 
  'REDIRECT_URL' => string '/toto' 
  'GATEWAY_INTERFACE' => string 'CGI/1.1'
  'SERVER_PROTOCOL' => string 'HTTP/1.1' 
  'REQUEST_METHOD' => string 'GET' 
  'QUERY_STRING' => string '' 
  'REQUEST_URI' => string '/toto' 
  'SCRIPT_NAME' => string '/index.PHP' 
  'PHP_SELF' => string '/index.PHP' 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)