Forum Moderators: phranque

Message Too Old, No Replies

mapping subdomains to subdirectory

         

powerflash

9:57 am on Dec 20, 2014 (gmt 0)

10+ Year Member



I need of rewrite all subdomains (wildcard dns is enabled) to subdirectory, for example:

- subdomain1.example.com -> example.com/subdomain1/
- subdomain-2.example.com -> example.com/subdomain-2/
...

I tried with htaccess but I receive a 502 proxy error, i used this code:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail|smtp|pop3)$ [NC]
RewriteRule ^.*$ http://example.com/%1 [R,P]

I don't can install mod_proxy why I read that is incompatible with cpanel vps

thank you for help
Alessio

[edited by: phranque at 11:05 am (utc) on Dec 20, 2014]
[edit reason] exemplified domain [/edit]

powerflash

11:10 am on Dec 20, 2014 (gmt 0)

10+ Year Member



ops, I confirm that proxy is enabled on my cpanel server, but I get 502

powerflash

12:09 pm on Dec 20, 2014 (gmt 0)

10+ Year Member



I think solved

I added this in httpd.conf, in vhost directives

ProxyPass /(.*) http://example.com/
ProxyPassReverse /(.*) http://example.com/

and added this in htaccess

RewriteCond %{HTTP_HOST} !^(www|ftp|imap|mail|pop|smtp)\.
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,P,QSA]

work fine :-)

[edited by: phranque at 1:16 am (utc) on Dec 21, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

phranque

1:25 am on Dec 21, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the QSA flag is only necessary when the RewriteRule directive's Substitution string contains a query string and you also want to append the query string from the request.

RewriteCond %{HTTP_HOST} !^(www|ftp|imap|mail|pop|smtp)\.
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com

i would suggest being as specific as possible with your regular expressions:
RewriteCond %{HTTP_HOST} !^(www|ftp|imap|mail|pop|smtp)\.example\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$

lucy24

7:10 am on Dec 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Odd. The original post used better wording, which seems to have been lost in transit.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail|smtp|pop3)$ [NC]

-- focusing on ([^\.]+) -- would work fine, although personally I'd just use \w+ instead of [^\.]+

phranque, he doesn't really need to exclude ftp, pop3 and so on, does he? I thought htaccess rules applied only to http requests.

In any case there's no point to ".*" because how often do you get a request beginning in "http://.example.com" -- and if you did, would you want to honor it?

phranque

7:58 am on Dec 21, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



phranque, he doesn't really need to exclude ftp, pop3 and so on, does he? I thought htaccess rules applied only to http requests.


that is a question for powerflash.
there could be a requirement to accept web requests on those hostnames.