URI 重写到子域

Posted

技术标签:

【中文标题】URI 重写到子域【英文标题】:URI Rewrite to Subdomain 【发布时间】:2019-02-06 12:01:43 【问题描述】:

我尝试将 URI 更改为子域。怎么把www.domain.com/auth/signin改成login.domain.com

我有这样的虚拟主机:

<VirtualHost 127.0.0.5:80>
   DocumentRoot "path/to/root/public"
   ServerName domain.local
   ServerAlias *.domain.local
</VirtualHost>

.htaccess

RewriteEngine On
php_flag display_errors 1
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^ index.php [QSA,L]

RewriteCond %HTTP_HOST !^domain.local
RewriteCond %HTTP_HOST ^(.+).domain.local
RewriteRule ^login/?$ http://domain.local/auth/signin [P,L,QSA]

【问题讨论】:

你的规则是不是颠倒了? RewriteRule ^auth/signin$ login.domain.com [P,L,QSA] @R.Martin 仍然不起作用。请试着解释一下你的意思 【参考方案1】:

1:标准http版本:

RewriteEngine On
php_flag display_errors 1
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^ index.php [QSA,L]

# If the url is not http://domain.local (Added [NC])
RewriteCond %HTTP_HOST !^domain\.local [NC]

# If the url is not http://www.domain.local (Added [NC])
RewriteCond %HTTP_HOST !^www\.domain\.local [NC]

# Redirect
RewriteRule ^login(.*)$ http://domain.local/auth/signin$1 [R=301,L,NC]

http://sub1.domain.local/login 到 http://domain.local/auth/signin

http://sub2.domain.local/login 到 http://domain.local/auth/signin

http://sub2.domain.local/login?token=1 到 http://domain.local/auth/signin?token=1

2:(可选)http 或 https 检测

RewriteEngine On
php_flag display_errors 1
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^ index.php [QSA,L]

# If the url is not http(s)://domain.local (Added [NC])
RewriteCond %HTTP_HOST !^domain\.local [NC]

# If the url is not http(s)://www.domain.local (Added [NC])
RewriteCond %HTTP_HOST !^www\.domain\.local [NC]

# http or https detection
RewriteCond %HTTPS off [OR]
RewriteCond %HTTPS:s on:(s) 

# Redirect
RewriteRule ^login(.*)$ http%1://domain.local/auth/signin$1 [R=301,L,NC]

http://sub1.domain.local/login 到 http://domain.local/auth/signin

https://sub1.domain.local/login 到 https://domain.local/auth/signin

http://sub2.domain.local/login 到 http://domain.local/auth/signin

https://sub2.domain.local/login 到 https://domain.local/auth/signin

http://sub2.domain.local/login?token=1 到 http://domain.local/auth/signin?token=1

https://sub2.domain.local/login?token=1 到 https://domain.local/auth/signin?token=1

【讨论】:

以上是关于URI 重写到子域的主要内容,如果未能解决你的问题,请参考以下文章

从子域重写而不更改 url

.htaccess 将子域重写到目录并将子域保留在 url

URL重写到子域

.htaccess 将子域重写到目录

nginx子域重写

htaccess 子域重写规则