Redirect subdomain to sub-folder

霸气de小男生 提交于 2019-12-19 11:19:34

问题


I need to write rewrite rule, to redirect subdomains to sub-folder.

Example: subdomain.domain.io should be redirected to domain.io/apps/subdomain, and test7.domain.io should be redirected to domain.io/apps/test7 and so on...

in .htaccess file I can catch the subdomain via following condition:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.io$
RewriteRule ?

But how to make redirection?

Thanks


回答1:


You can use this rule in your site root .htaccess:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.domain\.io$ [NC]
RewriteRule ^ apps/%1%{REQUEST_URI} [L]
  • RewriteCond %{ENV:REDIRECT_STATUS} ^$ is used to prevent looping of this rule.
  • %1 is back-reference of subdomain text captured in 2nd RewriteCond.


来源:https://stackoverflow.com/questions/47580375/redirect-subdomain-to-sub-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!