If you want to redirect a subdomain like sub.domain.tld into a subdirectory of the website and keep the original URL in the browser location bar, you may use the following apache directives.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.tld [NC]
RewriteRule ^/(.*)$ /sub/$1 [L]

This rewrite rule can be added into a .htaccess file in the website root or inside the vhost file. If you use ISPConfig 2 or 3, you can add this also into the apache directives field in the website settings.

Replace sub.domain.tld with the subdomain that shall be redirected and /sub/ with the path to the directory were the pages for this subdomain are located.

Redirect a subdomain with apache mod_rewrite and keep the URL in the address bar
Facebooktwittergoogle_plusredditlinkedinmail
Tagged on:             

6 thoughts on “Redirect a subdomain with apache mod_rewrite and keep the URL in the address bar

  • September 18, 2010 at 4:19 pm
    Permalink

    What about main subdomain, will it work or do I have to give always argument?

    Reply
  • September 19, 2010 at 11:31 am
    Permalink

    The rewrite rule should work for any domain name, it does not matther if its a second or third level domain.

    Reply
  • December 11, 2011 at 4:08 pm
    Permalink

    I too find this to be a bogus rule and does not work

    Reply
    • December 12, 2011 at 12:29 pm
      Permalink

      The rule works fine. If it downt work on your system then you either made a typo somewhere or you specified a wrong (sub)domain.

      Reply
  • October 10, 2015 at 2:07 am
    Permalink

    If you want to keep the subdomain in the browser bar, you have to add a [P] tag (for proxy).


    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^sub.domain.tld [NC]
    RewriteRule ^/(.*)$ /sub/$1 [P,L]

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *