URI重写为子域

时间:2018-08-31 12:41:35

标签: apache .htaccess xampp vhosts

我尝试将URI更改为Subdomain。如何将www.domain.com/auth/signin更改为login.domain.com

我有这样的virtualhost:

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

1 个答案:

答案 0 :(得分:0)

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/loginhttp://domain.local/auth/signin

     

http://sub2.domain.local/loginhttp://domain.local/auth/signin

     

http://sub2.domain.local/login?token=1http://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/loginhttp://domain.local/auth/signin

     

https://sub1.domain.local/loginhttps://domain.local/auth/signin

     

http://sub2.domain.local/loginhttp://domain.local/auth/signin

     

https://sub2.domain.local/loginhttps://domain.local/auth/signin

     

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

     

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