How to rewrite rules for sub-domains

2019-09-07 08:32发布

问题:

I recently registered a domain name kbcsurveyors.com. Then, I created two sub-domains, which created two new folders in the root folder.

My motive is that if I type kbcsurveyors.com/preinspection, it should point to preinspection.kbcsurveyors.com. Same for other sub domains.

In my .htaccess, which I placed inside root of mydomainname.com, I have written following lines:

RewriteEngine  On                                     
RewriteBase /
RewriteRule ^preinspection/(.*)$          https://kbcsurveyors.com/$1 [L,NC,QSA]    # Handle requests for "Preinspection"

But this file structure does not work.

How do I write the .htaccess file to achieve what I want?

Regards

回答1:

Remove or comment out your existing rule.

Create a new file preinspection/.htaccess with this rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^preinspection\.kbcsurveyors\.com$ [NC]
RewriteEngine .* http://preinspection.kbcsurveyors.com/$0 [L,NE,R=301]