.htaccess 子域的端口重写

.htaccess port rewrite for subdomains

我正在尝试将所有子域(portal.example.com 除外)从任何端口(通常是 80 或 443)重定向到端口 30000。

例如

我目前有以下 .htaccess,指导 example.com 和 *.example.com 不变:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

能否请您尝试按照您显示的示例编写。请确保在测试 URL 之前清除浏览器缓存。

RewriteEngine ON
RewriteCond !^example\.com [NC]
RewriteCond !^www\.example\.com [NC]
RewriteCond !^portal\.example\.com [NC]
RewriteCond (.*example\.com) [NC]
RewriteCond %{SERVER_PORT} !^30000$
RewriteRule ^(.*)$ http://%{HTTP_HOST}:30000%{REQUEST_URI} [R=301,NE,L]

稍微重构的规则:

RewriteEngine On

RewriteCond %{SERVER_PORT} !=30000
RewriteCond !^((?:www|portal)\.)?example\.com [NC]
RewriteRule ^ http://%{HTTP_HOST}:30000%{REQUEST_URI} [R=301,NE,L]