Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

php - How do I make a subdomain redirect to a dedicated folder?

For example, how do I make test.domain.com redirect to Subomains/test or test2.domain.com redirect to Subdomains/test2 without changing the test.domain.com and without creating a subdomain in cPanel?

Could I make .htaccess get the subdomain and redirect it to the folder?

And when there's no folder named as the subdomain, it just redirects to domain.com

question from:https://stackoverflow.com/questions/65649961/how-do-i-make-a-subdomain-redirect-to-a-dedicated-folder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is definitely possible to redirect via .htaccess. You can do this by changing the following code for your subdomain.

# Do not change this line.
RewriteEngine on
  
# Change subdomain.yourdomain.com to be your domain.
RewriteCond %{HTTP_HOST} ^(subdomain.)?yourdomain.com$
  
# Change 'subfolder' to be the folder you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
  
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
  
# Change 'subfolder' to be the folder you will use for your main domain.
RewriteRule ^(.*)$ /subfolder/$1
  
# Change yourdomain.com to be your main domain again.
# Change 'subfolder' to be the folder you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
  
RewriteCond %{HTTP_HOST} ^(subdomain.)?yourdomain.com$
RewriteRule ^(/)?$ subfolder/index.html [L]

Please see: .htaccess to make a subfolder the main folder for primary domain, but getting redundant redirects when URL is typed directly into browser


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...