I'm using a server which allows me to create and redirect subdomains. However, it is reflected on the address URL bar.

What I want is to keep subdomain.domain.com as base URL for the subdirectories inside domain.com/subdirectory/

Searching on the internet I always come up with these lines for .htaccess, which I think it does the redirection, but not the masking.

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ sub/$1 [L]


Tried this, but didn't work

RewriteEngine  on
RewriteBase    /subdirectory/
RewriteRule    ^(.*)$  http://subdomain.domain.com/$1  [P]
ProxyPassReverse /subdirectory/ http://subdomain.domain.com/

1 Answer 1


What you are asking for is solved using mod_proxy with mod_redirect. Research this Apache documentation page: http://httpd.apache.org/docs/current/rewrite/proxy.html

I do warn you that you have to be careful when configuring your Apache to mod_proxy. Here is the Apache documentation on that. There is a strong warning on the top of the page. But do not let that scare you. Just be careful and thorough.

Having said that, I used to use this feature A LOT! If configured safely, it works extremely well, is seamless, and highly reliable.

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/sub [NC]
RewriteRule .* sub.domain.com [P,L]

You do not need RewriteBase unless it is something other than the default.

I think this example will work for you. If not, I will change the example and we will try again. To test, use [R,L] instead of [P,L].

  • Ok, but my subdomain points to a subdirectory inside my local public_html. Is it the same? Commented Jun 24, 2014 at 21:15
  • Exactly the same. Instead of R (redirect) you use P (proxy). You are not using R for redirect in your example above. It really should be [R,L]. I think that is okay because redirect is assumed. However, in the new case, you would replace the R with P to proxy instead of redirect [P,L]. It is a slick as snot option if mod_proxy is configured to be safe.
    – closetnoc
    Commented Jun 24, 2014 at 21:20
  • But I need to proxy the folder to -> the subdomain, not the subdomain to the folder. I think that the example above is not what I'm looking for. Commented Jun 24, 2014 at 21:22
  • Oh yeah! I messed that. Sorry. Hold on. I will update the answer with an example that might work better.
    – closetnoc
    Commented Jun 24, 2014 at 21:29
  • Thanks for your time! I've edited my question with another try Commented Jun 24, 2014 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.