Search code examples
.htaccessmod-rewriteurl-rewritingmod-proxy

.htaccess rewrite dynamic url in subdomain


I have this url:

http://www.example.com/search.php?&location=ca&co=us

I would like rewrite so:

ca.example.com

In htaccess I placed this code but not working:

RewriteCond %{HTTP_HOST} ^([^/]*)\.example\.com$ [NC]
RewriteRule (.+)$ /search.php?location=$1&co=us [L,P]

I dont understand where Im wrong.


Solution

  • Back reference from RewriteCond is %1, %2 etc. You can use:

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(.+?)\.(vincentjob\.com)$ [NC]
    RewriteRule ^ http://www.%2/search.php?location=%1&co=us [L,P]
    

    Assuming mod_proxy is setup and running.